You might want to hide the named ranges in your workbook to prevent your users to tamper with your calculations. In this article, we're going to show you how to hide named ranges in Excel using VBA.
How to hide named ranges using VBA
Named ranges are objects in a workbook's Names collection and they have a property called Visible. The Visible property determines the visibility status of a worksheet as its name refers. This property can take a Boolean value: TRUE or FALSE
True and False values represent the visible and hidden statuses respectively. The hidden named ranges are useful for when you need the source of the named range to remain in the workbook. The named ranges can be made visible or hidden only by VBA or other third party add-ins.
You can use codes in two ways:
- Module
- Immediate Window
In the Module method, you need to add the module into the workbook or the add-in file. Copy and paste the code into the module to run it. The main advantage of the module method is that it allows saving the code in the file, so that it can be used again later. Furthermore, the subroutines in modules can be used by icons in the menu ribbons or keyboard shortcuts. Remember to save your file in either XLSM or XLAM format to save your VBA code.
The Immediate Window method, on the other hand, is essentially a quick and dirty method where you can simply copy and paste the code into the Immediate Window and press the Enter key to run it. Unfortunately, any code you use in the Immediate Window will not be saved. Also note that icons and keyboard shortcuts will not be available.
Hide all named ranges
Module Version:
Sub HideAllNames() Dim nm As Name For Each nm In ActiveWorkbook.Names nm.Visible = False Next End Sub
Immediate Window version:
For Each nm In ActiveWorkbook.Names: nm.Visible = False: Next
Hide a specific named range
Module Version:
Sub HideSpecificName() ActiveWorkbook.Names("MyName").Visible = False End Sub
Immediate Window version:
ActiveWorkbook.Names("MyName").Visible = False