Pramod
Pramod

Reputation: 1461

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

Upvotes: 2

Views: 26008

Answers (1)

Dick Kusleika
Dick Kusleika

Reputation: 33145

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.

Upvotes: 4

Related Questions