Pass data from one userform to another

GiraffetheGeek

Board Regular
Joined
May 25, 2011
Messages
58
Hi there

I have a userform that has 20 lines of 4 text boxes and one button on each line.

I want the button to open a second userform that has only one line of 4 text boxes and copy the text in it line of text boxes to the text boxes in the second user form.

I have declared these at the top of the first userform's code
Code:
Public NameToPass As String
Public DisToPass As String
Public HelpToPass As String
Public CMToPass As String

The code I have for the button is
Code:
Private Sub Btn_Week1Appt1_Click()
    NameToPass = WB_TxtBox_NameWeek1Appt1.Text
    DisToPass = WB_TxtBox_DisWeek1Appt1.Text
    HelpToPass = WB_TxtBox_HelpWeek1Appt1.Text
    CMToPass = WB_TxtBox_CMWeek1Appt1.Text
    Enter_Appt.Show
        
End Sub

And the code I have for the initialise function on the second userform is
Code:
Private Sub UserForm_Initialize()
    Box_EnterName.Text = NameToPass
    Box_EnterDis.Text = DisToPass
    Box_EnterHelp.Text = HelpToPass
    Box_EnterCM.Text = CMToPass
End Sub

However nothing is being put into the text boxes on the second userform. I know the varibale NameToPass, DisToPass etc are being populated correctly so I figure it has something to do with going from one form to another.

I suppose I could copy the data to a sheet first and then pull the data from the sheet to the new user form, but would like to avoid that and teach myself how to pass data from one form to another.

Cheers in advance
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I have declared these at the top of the first userform's code
Code:
Public NameToPass As String
Public DisToPass As String
Public HelpToPass As String
Public CMToPass As String

For the moment, move these to the top of a Standard Module.

Am I correct in presuming that the userforms are modeless?
 
Upvote 0
If modeless is the default when creating userforms in excel then they are.

I am new to using userforms so am learning as I go.

How do I tell if they are modeless or not?
 
Upvote 0
If modeless is the default when creating userforms in excel then they are... How do I tell if they are modeless or not?

No, userforms are Modal by default. This means that you cannot click back onto the worksheet while the form is up. If you show a form in Modeless, then you can change focus (to let's say, the worksheet) while the form is running.

To check or set this property in design mode, with the userform selected, look at the properties window and look for ShowModal.

To set the property during runtime:
Code:
UserForm1.Show vbModal
'or 
UserForm.Show vbModeless

I am afraid I was presuming a bit, as I do not normally run multiple forms at the same time.

Mark
 
Upvote 0
This is one way to pass data from one userform to the other.
This is in the first userform's code module
Code:
Private Sub CommandButton2_Click()
    UserForm2.Tag = Join(Array(NameToPass, DisToPass, HelpToPass, CMToPass), ",")
    
     UserForm2.Show
End Sub

This is in Userform2's Activate event.

Code:
Private Sub UserForm_Activate()
    Dim Arguments As Variant
    
    With Me
        Arguments = Split(.Tag, ",")
        ReDim Preserve Arguments(0 To 3)
        
        .TextBox1 = Arguments(0)
        .TextBox2 = Arguments(1)
        .TextBox3 = Arguments(2)
        .TextBox4 = Arguments(3)
    End With
End Sub
 
Last edited:
Upvote 0
Or, since you have those variables declared public, scrub the Activate event above and use this in UserForm2.
Code:
Private Sub UserForm_Initialize()
    Me.TextBox1.Text = UserForm1.NameToPass
    Me.TextBox2.Text = UserForm1.DisToPass
    Me.TextBox3.Text = UserForm1.HelpToPass
    Me.TextBox4.Text = UserForm1.CMToPass
End Sub

Public variables that are declared in an object's code module (userform,sheet,Thisworkbook) can be treated as properties of that object and can be read or written to as such.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,777
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top