Within Excel VBA I have a User Form similar to the following where the user enters an ID number and then the details are displayed on the user form:
Private
The most simplest way is:
UserForm2.TxtIDNo.Text = UserForm1.txtIDNo.Text
There are many many ways... Here are some...
Way 1
Public
Variable in a ModuleIn Userform1
Private Sub CommandButton1_Click()
MyVal = "Sid"
UserForm2.Show
End Sub
In Userform2
Private Sub CommandButton1_Click()
MsgBox MyVal
End Sub
In Module
Public MyVal
Way 2
Use the .Tag
property of the userform
In Userform1
Private Sub CommandButton1_Click()
UserForm2.Tag = "Sid"
UserForm2.Show
End Sub
In Userform2
Private Sub CommandButton1_Click()
MsgBox Me.Tag
End Sub
Way 3
Add a Label
in Userform2 and set it's visible property to False
In Userform1
Private Sub CommandButton1_Click()
UserForm2.Label1.Caption = "Sid"
UserForm2.Show
End Sub
In Userform2
Private Sub CommandButton1_Click()
MsgBox Label1.Caption
End Sub
There are serveral ways to solve this problem. The one that I use is declare global or public variable in module
Example:
Public commonVariable As String
then in userform you can assign or retrieve value from this variable. For example in userform1:
Private Sub btnIDNo_Click()
commonVariable = "UserId"
End Sub
in UserForm2:
Private Sub CommandButton1_Click()
me.txtIDNo.Text = commonVariable
End Sub