Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Hiding Macros.
by Allen Wyatt
(last updated July 4, 2015)
Most readers already know that you can create functions and subroutines using VBA. This is no different than it is under VBA's namesake, Visual Basic. Normally, a macro shows up in the macro list when you display the Macros dialog box (press Alt+F8), unless one of three conditions is met:
The only type of macro listed in the Macros dialog box is a non-private subroutine with no parameters. In certain situations, however, you may not want those listed either. For instance, you may have created some universal subroutines that don't do anything useful if called on their own; they are designed to be called from other code. For instance, consider the following macro:
Sub MySub() MsgBox "We are running the macro" End Sub
This macro will appear in the Macros dialog box. If you don't want it to appear, there are several solutions you can pursue, all of which become obvious from examining the three ways in which macros are excluded from the macro list. The first potential solution is to examine your code and find out if it is really "universal." Do you need the code from more than a single module? If you don't, then declare the subroutine Private; it will not appear in the Macros dialog box. Thus, the previous problem macro becomes the following:
Private Sub MySub() MsgBox "We are running the macro" End Sub
The second way to hide the macro is to simply convert it to a function. This may sound odd, particularly if you don't want to return any values, but it is perfectly permissible. In VBA a function does not have to return a value. In the absence of explicitly declaring a return value, the function will return a default result (for example, Boolean returns False, String returns "", etc.) Thus, the problem procedure could be changed to a function and declared as shown here:
Function MySub() As Boolean MsgBox "We are running the macro" End Function
This procedure doesn't show in the Macros dialog box and does not require arguments. It returns False by default, but this result can be ignored. Depending on the nature of the subroutine you are changing, it may be to your benefit to really allow the converted function to return True or False depending on the success of what is being done in the code. In this case, the converted function is a real function, and not really a dummy subroutine, since it is returning something of value.
The third potential solution is to use some dummy parameters with the subroutine. You don't need to do anything with them within the subroutine itself, but by including them the procedure is not listed in the macro list. In this scenario, the problem subroutine is changed to something like the following:
Sub MySub(Void As Integer) MsgBox "We are running the macro" End Sub
Now the procedure is not listed in the macro list, but you need to change the way in which the subroutine is called. You must modify every instance so that a parameter is passed, even though it is never used.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9904) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Hiding Macros.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Macros depend on the use of variables to do their work. This tip examines how variables are declared in a macro, using ...Discover More
When you have a macro that processes a huge amount of data, it can seem like it takes forever to finish up. These ...Discover More
Many times you need to select just the visible cells before taking some action. It is helpful to know how to make this ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.