编译错误:子或函数未在 vba 中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12666778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:07:04  来源:igfitidea点击:

Compile Error: Sub or Function not defined in vba

excelvba

提问by Pramod

I have a userform with three buttons. When Ok button is clicked i wrote the below code:

我有一个带有三个按钮的用户表单。单击确定按钮时,我编写了以下代码:

Private Sub CommandButton2_Click()
   Call calculateCost
End Sub

and the sub method i wrote in the Sheet1 is:

我在 Sheet1 中写的子方法是:

Public Sub calculateCost()
    Dim kilo As String
    kilo = Worksheets("Sheet1").TextBox1.Text
    MsgBox "value" & kilo
End Sub

When im running the code im getting the error. Sub or function not defined near call calculateCost. Why is this happening? How to resolve this. Thankyou

当我运行代码时,我收到了错误。子或函数未定义近调用计算成本。为什么会这样?如何解决这个问题。谢谢

回答by Dick Kusleika

Move your calculatedCost procedure to a standard module (Insert - Module in the VBE). Procedures in a class module, including a sheet class module, can't be called like that. You can call it from a sheet's class module in other ways, but I don't see anything in your procedure that requires it be in the sheet's module, so it probably belongs in a standard module.

将您的计算成本过程移动到标准模块(插入 - VBE 中的模块)。类模块中的过程,包括表类模块,不能这样调用。您可以通过其他方式从工作表的类模块中调用它,但我在您的过程中没有看到任何需要它在工作表模块中的内容,因此它可能属于标准模块。