Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
can a userform pass an argument back to a module? if so, could you please
elaborate? Thanks. Mike |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
Hi
one way: use a public variable -- Regards Frank Kabel Frankfurt, Germany "smokiibear" schrieb im Newsbeitrag ... can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
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 Kabel" wrote in news:uuUZMB82EHA.2572 @tk2msftngp13.phx.gbl: Hi one way: use a public variable -- Regards Frank Kabel Frankfurt, Germany "smokiibear" schrieb im Newsbeitrag ... can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
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 Kabel" wrote in news:uuUZMB82EHA.2572 @tk2msftngp13.phx.gbl: Hi one way: use a public variable -- Regards Frank Kabel Frankfurt, Germany "smokiibear" schrieb im Newsbeitrag ... can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
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) -- Regards Frank Kabel Frankfurt, Germany "smokiibear" schrieb im Newsbeitrag ... 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 Kabel" wrote in news:uuUZMB82EHA.2572 @tk2msftngp13.phx.gbl: Hi one way: use a public variable -- Regards Frank Kabel Frankfurt, Germany "smokiibear" schrieb im Newsbeitrag ... can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
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. "Frank Kabel" wrote in news:eaCW1a82EHA.1396 @tk2msftngp13.phx.gbl: 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) -- Regards Frank Kabel Frankfurt, Germany "smokiibear" schrieb im Newsbeitrag ... 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 Kabel" wrote in news:uuUZMB82EHA.2572 @tk2msftngp13.phx.gbl: Hi one way: use a public variable -- Regards Frank Kabel Frankfurt, Germany "smokiibear" schrieb im Newsbeitrag ... can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
Mike,
smokiibear wrote: 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. 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] |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
"Jake Marx" wrote in
: Mike, smokiibear wrote: 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. 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. |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
Hi Mike
I really dislike the use of public variables for those purposes, so allow me to mention an alternative solution. Variables to be passed to a Sub or Function are placed withinits parentheses: Sub Test(S As String) MsgBox "Someone passed " & S End Sub Now in your userform: Private Sub CommandButton1_Click() Call Test(Me.TextBox1.Text) End Sub HTH. Best wishes Harald "smokiibear" skrev i melding ... can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
"Harald Staff" wrote in message
... Hi Mike I really dislike the use of public variables for those purposes, so allow me to mention an alternative solution. Variables to be passed to a Sub or Function are placed withinits parentheses: Sub Test(S As String) MsgBox "Someone passed " & S End Sub Now in your userform: Private Sub CommandButton1_Click() Call Test(Me.TextBox1.Text) End Sub HTH. Best wishes Harald Hi Harald, How about if the sub calls the userform (rather than the other way around as you have shown above)? What is the best practice way to pass a variable back to the calling sub? Thanks, Alan. |
#11
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
Another alternative (I don't like globals either) is to set an invisible
label's caption on the form to return the value you want. Sub Launch frm1.Show strReturn = frm1.lblValue.Caption Unload frm1 End Sub in form code cmdOK_Click lblValue.Caption = "Something" Me.Hide End Sub Robin Hammond www.enhanceddatasystems.com "smokiibear" wrote in message ... can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#12
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
The "official" way to pass information between a form and a procedure is to create
properties for the form using property procedures. How about a simple example? I have a simple userform, UserForm1, with two option buttons (captions Yes and No) and a commandbutton (Close). The close button only hides the form, doesn't unload it, so it and its buttons are still in memory. Here is the code behind the form: '================================================= ===== ' UserForm1 Code Module Option Explicit Public Property Get YesOrNo() As String Select Case True Case Me.OptionButton1.Value YesOrNo = "Yes" Case Me.OptionButton2.Value YesOrNo = "No" End Select End Property Public Property Let YesOrNo(ByVal sNewValue As String) If UCase$(sNewValue) = "NO" Then Me.OptionButton2.Value = True Else Me.OptionButton1.Value = True End If End Property Private Sub CommandButton1_Click() Me.Hide End Sub '================================================= ===== Property Let means the procedure can assign a value to the property by saying, e.g., UserForm1.YesOrNo = "Yes". When you assign the property in this way, the code within the Let procedure is run, which in this case, selectes the appropriate option button. Property Get is how the procedure learns the value of the property, using UserForm1.YesOrNo to run the Get procedure, which in this case returns Yes or No based on which option button is selected. Here's the code within a simple procedure in a regular code module. It first asks a Yes or No questions, instantiates the form, sends the preliminary response to the form and shows the form. When the form is hidden, it asks the form what the final answer is, and displays that. '================================================= ===== ' Regular procedure in a regular code module Sub Test() Dim lResponse As Long ' pass to form Dim sResponse As String ' get from form Dim frm1 As UserForm1 'ask user for initial setting lResponse = MsgBox("Yes or No", vbQuestion + vbYesNo) ' turn setting into a string sResponse = IIf(lResponse = vbYes, "Yes", "No") ' start up the form Set frm1 = New UserForm1 With frm1 ' pass variable to form .YesOrNo = sResponse .Show ' get new value back from the form sResponse = .YesOrNo End With Unload frm1 ' tell user what new value is MsgBox "Form returned " & sResponse, vbExclamation End Sub '================================================= ===== - Jon ------- Jon Peltier, Microsoft Excel MVP Peltier Technical Services Tutorials and Custom Solutions http://PeltierTech.com/ _______ smokiibear wrote: can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
#13
Posted to microsoft.public.excel.programming
|
|||
|
|||
can a userform pass an argument?
I've punched up my description below, and now it resides on my web site:
http://peltiertech.com/Excel/PropertyProcedures.html - Jon ------- Jon Peltier, Microsoft Excel MVP Peltier Technical Services Tutorials and Custom Solutions http://PeltierTech.com/ _______ Jon Peltier wrote: The "official" way to pass information between a form and a procedure is to create properties for the form using property procedures. How about a simple example? I have a simple userform, UserForm1, with two option buttons (captions Yes and No) and a commandbutton (Close). The close button only hides the form, doesn't unload it, so it and its buttons are still in memory. Here is the code behind the form: '================================================= ===== ' UserForm1 Code Module Option Explicit Public Property Get YesOrNo() As String Select Case True Case Me.OptionButton1.Value YesOrNo = "Yes" Case Me.OptionButton2.Value YesOrNo = "No" End Select End Property Public Property Let YesOrNo(ByVal sNewValue As String) If UCase$(sNewValue) = "NO" Then Me.OptionButton2.Value = True Else Me.OptionButton1.Value = True End If End Property Private Sub CommandButton1_Click() Me.Hide End Sub '================================================= ===== Property Let means the procedure can assign a value to the property by saying, e.g., UserForm1.YesOrNo = "Yes". When you assign the property in this way, the code within the Let procedure is run, which in this case, selectes the appropriate option button. Property Get is how the procedure learns the value of the property, using UserForm1.YesOrNo to run the Get procedure, which in this case returns Yes or No based on which option button is selected. Here's the code within a simple procedure in a regular code module. It first asks a Yes or No questions, instantiates the form, sends the preliminary response to the form and shows the form. When the form is hidden, it asks the form what the final answer is, and displays that. '================================================= ===== ' Regular procedure in a regular code module Sub Test() Dim lResponse As Long ' pass to form Dim sResponse As String ' get from form Dim frm1 As UserForm1 'ask user for initial setting lResponse = MsgBox("Yes or No", vbQuestion + vbYesNo) ' turn setting into a string sResponse = IIf(lResponse = vbYes, "Yes", "No") ' start up the form Set frm1 = New UserForm1 With frm1 ' pass variable to form .YesOrNo = sResponse .Show ' get new value back from the form sResponse = .YesOrNo End With Unload frm1 ' tell user what new value is MsgBox "Form returned " & sResponse, vbExclamation End Sub '================================================= ===== - Jon ------- Jon Peltier, Microsoft Excel MVP Peltier Technical Services Tutorials and Custom Solutions http://PeltierTech.com/ _______ smokiibear wrote: can a userform pass an argument back to a module? if so, could you please elaborate? Thanks. Mike |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
pass argument to macro | Excel Programming | |||
Pass an argument to Excel workbook | Excel Programming | |||
How to pass an Excel range as an argument to a SQL Server stored Procedure | Excel Programming | |||
How to pass values from a userform to a standard module? | Excel Programming | |||
How to pass arguments from ThisWorkbook to a UserForm | Excel Programming |