简体   繁体   English

在Excel中禁用自定义功能

[英]Custom Function Disable in Excel

I have created a custom function that I am using excplicitly in another module in VBA. 我创建了一个自定义函数,正在VBA的另一个模块中明确使用它。 Function looks something like this: 函数看起来像这样:

Function Coverts(ByVal inputString As String) As String
   'Coverts code here
End Function

It works perfectly fine in both VBA and Excel UI. 在VBA和Excel UI中都可以正常工作。 However, I do not want it to work or appear in Excel UI as I only want to use it in VBA. 但是,我不希望它工作或出现在Excel UI中,因为我只想在VBA中使用它。 Is there a way to do this? 有没有办法做到这一点?

Thanks 谢谢

Put

Option Private Module

at the top of the module containing your function. 在包含您的功能的模块顶部。


From MSDN : 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. 当模块包含Option Private Module ,公共部分(例如,在模块级别声明的变量,对象和用户定义的类型)在包含该模块的项目中仍然可用,但不适用于其他应用程序或项目。

you can add the keywords Public or Private to your functions, subs or global variable to have that specified. 您可以在函数,子变量或全局变量中添加关键字“公共”或“专用”以指定关键字。

so if you want to only want this function to be accessible by your code and not in excel sheets you can add private: 因此,如果您只想通过代码而不是在Excel工作表中访问此功能,则可以添加private:

Private Function Coverts(ByVal inputString As String) As String
   'Coverts code here
End Function

You can make the function inoperable if called from the worksheet by identifying the Application.Caller . 如果从工作表中调用Application.Caller,则可以使该函数无法操作。 If called as a function from the XL UI, this will be Range (ie the cell the function is within). 如果是从XL UI中调用的函数,则将是Range(即函数所在的单元格)。

Function Coverts(ByVal inputString As String) As String

   If TypeName(Application.Caller) = "Range" then
      Coverts = cverr(xlerrna)
      exit function
   end if

   'Coverts code here

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM