PDA

View Full Version : passing string from userform to public module variable



kieran989
05-04-2017, 05:35 PM
Im trying to pass a string from a userform textbox to a public variable defined in a module. Cant for the life of me figure out whats happening. My code looks like this:

Module code


Option Explicit


Public test As String


Sub btn_showuserform()

userform1.Show
MsgBox test

End Sub


Userform code


Option Explicit

Sub btnOk_Click()

test = userformtextbox.Value
Unload Me

End Sub


For some strange reason it works if I show the message box. But If i dont show the messagebox the value isnt passing to the variable??
Also if i dont show the messge box but run the code from within in the userform code, the value does pass??

Leith Ross
05-04-2017, 09:33 PM
Hello kieran989,

Qualify the variable with the module name in the UserForm code. If the variable Test is in Module1 then you would use...


Option Explicit

Sub btnOk_Click()

Module1.test = userformtextbox.Value
Unload Me

End Sub