0

All,

I'm struggling with passing input variables from a userform to a module/sub. I've searched the form, but can't find a matching example.

E.g.

Form 1: Two input fields named TextBoxA and TextBoxB and 1 Submit button. Module 1: some code in which I would like to use the input variables of TextBoxA and TextBoxB

Code in userform:

Private Sub CommandButton1_Click()
    pass = myuserform.TextBoxA
End Sub

Who can point me in the right direction. Much appreciated.

2
  • If pass is the variable you want to access from a standard module, all you need is to declare pass as a public variable as "Public pass as String" on a standard module so that once you assign a value to the pass variable on userform, it can be accessed in any sub routine on a standard module Commented Oct 4, 2017 at 6:33
  • 1
    Examples here: mrexcel.com/forum/excel-questions/… Commented Oct 4, 2017 at 6:33

1 Answer 1

0

Setup a Method which accepts two parameters in a Standard Module and pass the TextBox values as arguments:

Public Sub TestMethod(ByVal param1 As Variant, ByVal param2 As Variant)
    MsgBox "You passed: " & param1 & " and " & param2
End Sub

Then you only need to call the method in your button's Click event:

Private Sub CommandButton1_Click()
    TestMethod myuserform.TextBoxA.Value, myuserform.TextBoxB.Value
End Sub
Sign up to request clarification or add additional context in comments.

Comments

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.