VBA - Combining two userforms' textbox values.

khattabY

New Member
Joined
Dec 14, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have created two userforms as shown below.
1639500626790.png
1639500681380.png




The first userform has data entries to fill in and for the "Number of Segments" field, you add the number of segments to be add then press on the "Add segments" button to open another userform that automatically creates labels and textboxes from the number entered. Now, what I need to do is join the two userforms so when I press Done on userform(1) it will be able to print the values entered from userform(2).

What I had in mind is to create a loop that goes through all the textboxes from userform 2 depending on the number of segments added and make the values of those textboxes = textboxes in userform 1. I am open to any ideas that can make this easier.

Thanks,
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi & welcome to MrExcel.

What I had in mind is to create a loop that goes through all the textboxes from userform 2 depending on the number of segments added
That's quite doable provided you don't use the default instance of the second userform. You need to "New" the userform using a pre-declared variable. The command button on your second userform only needs to hide that userform. Finally the code needs to take some precautions so the user isn't able to close the second userform with its upper right [X] close button, otherwise the data in the text boxes will be lost.

Code of the second userform should contain at least these procedures:
VBA Code:
Private Sub CommandButton1_Click()
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub


Usage example:
VBA Code:
Sub Example()

    Dim usf As UserForm2
    Set usf = New UserForm2
    usf.Show

    Dim Ctl As MSForms.Control
    For Each Ctl In usf.Controls
        If TypeName(Ctl) = "TextBox" Then
            '
            ' do your stuff here
            '
        End If
    Next Ctl
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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