How Can I Prevent The Suggestion of Custom VBA Functions When Writing Formulas in Excel?

廉价感情. 提交于 2019-12-10 21:22:05

问题


I am writing a range of VBA functions / subs that can be reused in a number of projects. The issue I have is that these functions are listed in the suggested formula functions when using sheets.

Making the functions private will prevent this, but what it will also do is remove the handy hints showing the parameters for the functions when called from a module other than where the function is stored.

Does anyone know of a way to prevent the suggestion of custom functions without making them private?


回答1:


Parameterless Public members of a standard module are one of two things:

  • Sub procedures and they're exposed as macros.
  • Function procedures and they're exposed as UDF's.

Note that members are implicitly Public if no access modifier is specified.

A Sub that has parameters can't be executed as a macro, so it won't show up as an available macro.

A Function that has side-effects (e.g. mutates some module/global state, or changes other cells' value) is bad code, and a UDF can't change another cell's value anyway.


Making the functions private will prevent this, but what it will also do is remove the handy hints showing the parameters for the functions when called from a module other than where the function is stored.

Making it Private not only "removes the handy hints", it makes the function inaccessible to other modules, and your code won't compile.

If your code has UDF's, put them all in the same standard module, e.g. UserFunctions, and make them explicitly Public for readability's sake.

If your code has macros, put them all in the same standard module, e.g. Macros, and make them explicitly Public for readability's sake.

If your code has functions and procedures that need to be Public (e.g. they're accessed from UDF's and/or macros), make them explicitly Public for readability's sake, and put them in standard modules named appropriately (i.e. avoid Helper and Manager modules, they inevitably become a dumping bag of whatever doesn't quite fit anywhere, and grow to a mess).

Then put this at the top:

Option Explicit
Option Private Module

That option (Private Module) prevents all public/exposed members from being picked up as macros and UDF's. Option Explicit should just be there anyway.


Another way is to implement logic in class modules; members of a class module can't be accessed without an instance of that class (i.e. an object), and therefore won't be exposed as macros/UDF's.



来源:https://stackoverflow.com/questions/38227475/how-can-i-prevent-the-suggestion-of-custom-vba-functions-when-writing-formulas-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!