Thread: userforms
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
[email protected] paul.robinson@it-tallaght.ie is offline
external usenet poster
 
Posts: 789
Default userforms

Hi
A public variable declared in a userform code module becomes an object
for that form, so that when you unload the userform, you lose the
value of the variable. You can hide Userform1 however,

In userform1 module you have this

Option Explicit
Public MyVar As String

Private Sub CommandButton1_Click()
MyVar = Me.TextBox1.Value
Me.Hide
UserForm2.Show
End Sub

And in Userform2 module you have this

Option Explicit

Private Sub CommandButton1_Click()
Unload Me
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Value = UserForm1.MyVar
End Sub

Since Userform1 is hidden when Userform2 is called, then Userform2 can
see MyVar as an object of Userform1.

You can also declare MyVar in a GENERAL code module. Now it is
genuinely Public. In Userform1 code module you now use

Private Sub CommandButton1_Click()
MyVar = Me.TextBox1.Value
Unload Me
End Sub

And for Userform2

Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Value = MyVar
End Sub

The first method is useful as it keeps the variable names with the
form and makes your code easier to read/maintain. I tend to only use
the first method.

regards
Paul

On Apr 17, 2:36*pm, ranswrt wrote:
I have 2 userforms set up. *How do I pass a value from one userform to the
other?
In userform1 I set a "public item as string" variable, but it didn't pass
that value to userform2 when it called userform2.

Thanks