View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Harald Staff[_8_] Harald Staff[_8_] is offline
external usenet poster
 
Posts: 28
Default Return a value from Userform.Show()?

One way, probably the best, is building a class where the userform is a part
of its dataset. I don't know how comfortable you are with classes.

So an easier faster way is this: On top of the userform module, create some
public variables, like

Public S as string
Public L as long
'as many as you need

Code for its OK button:

Private Sub CommandButton1_Click()
Me.S = Me.Textbox1.text
Me.L = Me.Combobox1.Listindex
Me.Hide
End Sub

Code for its cancel button:

Private Sub CommandButton2_Click()
Me.S = "cancelled"
Me.Hide
End Sub


And finally, main code i a standard module:

Sub Test
Dim UF as Userform1
Dim X as String, Y as Long
Set UF = New Userform1
Uf.Show
X = UF.S
Y = UF.L
Unload UF
If X = "cancelled" then
'something sad?
Else
Msgbox "You wrote " & X
End if
End Sub

HTH. Best wishes Harald



"Robert Crandal" skrev i melding
...
I have been relying heavily on global variables to return
a value (or values) from any Userform in my program.
Is this the only way to return a value from Userform.Show()?

Is it possible to try something like:

x = Userform.Show() ' ???

This might work for returning one value, but what if I
had multiple values to return from a userform?

Just curious what other options are available, because I
feel like the global variable method is not always very
efficient.

Thanks!