can a userform pass an argument?

  • Thread starter Thread starter smokiibear
  • Start date Start date
S

smokiibear

can a userform pass an argument back to a module? if so, could you please
elaborate?

Thanks.

Mike
 
Frank....I have globally declare my variables, but they are not passing
back with the values they are assigned in the form. Any ideas?

Mike
 
Frank....I have globally declare my variables, but they are not passing
back with the values they are assigned in the form. Any ideas?

Mike
 
Hi
if you have them globally declared it SHOULD work. Are you sure you
don't have declared them twice (globally AND in the form)
 
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,
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.

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

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
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

I resolved the problem, but not sure why one way worked and the other
didn't. I had something like this (using your example):

Private Sub cmdOK_Click()
MyVariable = userform1.myvariable.value
Hide
End Sub

however, when I followed your example for the private routine, then
declared

MyVariable = userform1.myvariable.value

in the test routine, it worked fine.
 
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

Hi Harald,

Superb - I hadn't thought of the difference between hiding and
unloading the userform.

Thanks alot,

Alan.
 

Ask a Question

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.

Ask a Question

Back
Top