S
smokiibear
can a userform pass an argument back to a module? if so, could you please
elaborate?
Thanks.
Mike
elaborate?
Thanks.
Mike
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
after an option explicit statement and prior to my sub () statement, i
decalare several variables, of which are being referenced in the form.
the form obtains the proper values, but the variables are cleared
when I hide the form.
Mike,
You are probably declaring the variables in the UserForm. Those
variables will only retain their values for the lifetime of the
UserForm. If you want the variables to be true Global variables, you
must declare them as Public in a standard module.
If you want to leave them in your UserForm, you have to grab the
values before unloading the UserForm. And you have to preface the
variable name with the codename of the UserForm:
Sub Test()
UserForm1.Show
MsgBox UserForm1.MyVariable
Unload UserForm1
End Sub
'/ code behind UserForm
Public MyVariable As String
Private Sub cmdOK_Click()
MyVariable="OK"
Hide
End Sub
Harald Staff said:Hi Alan
Best practice is probably what Jon posted. I keep things as small as
possible. Userform code:
'**********************************
Option Explicit
Public MyMessage As String
Private Sub CommandButton1_Click()
MyMessage = "Clicked " & Now
Me.Hide
End Sub
'**********************************
(Public inside an object is not a public variable, it's a new
property of the object.) Now in a module:
'**********************************
Sub test()
UserForm1.Show
MsgBox UserForm1.MyMessage
Unload UserForm1
End Sub
'**********************************
Note that the button only hides the form, it can't be unloaded until
the properties are read so our macro does that afterwards.
HTH. Best wishes Harald
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.