2

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

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It worked. But im now getting a runtime error while storing the textbox value into variable: object doesn't support this method or variable. Can you help me with this
@Pramod Instead of TextBox.Text try using TextBox.Value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.