Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure public VBA methods don't show up in the list of Excel macros

Tags:

excel

vba

In Excel VBA (2003), I've noticed that any Public or Friend Sub method in either a module or ThisWorkbook that doesn't have any arguments will show up as a Macro that can be run by the user. I.e. when the user goes to Tools --> Macro --> Macros... (or Alt+F8) the method will show up in the list that can be run.

For sanity's sake (organization, maintenance, etc) I do need to separate the code into modules. I'm hoping to find a non-hacky way to hide some methods from the user, but still allow them to be visible to other code modules. Note that all the code is contained within the same application, so no external code is called.

My current work-around is to use Functions that return a boolean instead of Subs, and just ignore the return value. eJames suggested the option of using an optional argument in the Sub, which will also hide the method from the list of macros.

Neither of these feel quite right, however, so any advice about how to structure a non-trivial Excel VBA application would be much appreciated.

like image 249
AR. Avatar asked Nov 17 '08 19:11

AR.


People also ask

How do I protect and hide VBA code in Excel?

In the VBA main menu, click on Tools | VBAProject Properties and under the tab "Protection", Check the box "Lock project for viewing" and enter the password. Save and exit the Excel file. When you open it next the code will be hidden.

How do I run a macro without showing steps?

To hide the actions of the macro from the screen, add Application. ScreenUpdating = False to the beginning of your code. Set it back to True at the end.


2 Answers

Add the following to the top of your module:

Option Private Module 

From MSDN:

When a module contains Option Private Module, the public parts, for example, variables, objects, and user-defined types declared at module level, are still available within the project containing the module, but they are not available to other applications or projects.

like image 147
Jason Z Avatar answered Sep 25 '22 14:09

Jason Z


One solution is to give the method an Optional argument.

Public Sub myPublicSub(Optional dummy_var As Integer)     ... End Sub
like image 21
e.James Avatar answered Sep 24 '22 14:09

e.James