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

后端 未结 5 578
渐次进展
渐次进展 2020-12-13 19:22

In Excel VBA (2003), I\'ve noticed that any Public or Friend Sub method in either a module or ThisWorkbook that

相关标签:
5条回答
  • 2020-12-13 19:57

    Just a small addendum to Jason Z's answer: methods hidden by Option Private Module are still visible if you use Application.Run() to invoke the method.

    0 讨论(0)
  • 2020-12-13 20:03

    One solution is to give the method an Optional argument.

    Public Sub myPublicSub(Optional dummy_var As Integer)
        ...
    End Sub
    0 讨论(0)
  • 2020-12-13 20:06

    My methods have already been listed however, ChrisB made the following statement:

    Perhaps we should all have Option Private Module at the tops of our code modules as a rule – except where the module contains procedures called by buttons

    Even Private routines can be called from a button if the Macro is assigned to the button before making it private.

    0 讨论(0)
  • 2020-12-13 20:10

    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.

    0 讨论(0)
  • 2020-12-13 20:10

    Also worth noting a side effect of both Option Private Module and adding Optional Params is that the Sub will no longer work as a target of a shortcut keybinding.

    Restated: If you have a keybinding to set a keyboard shortcut to launch a Macro. That Macro cannot be hidden. Otherwise the keybinding will not work.

    0 讨论(0)
提交回复
热议问题