简体   繁体   中英

Compile Error: Sub or Function not defined in vba

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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