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