autofill userform textboxes. next empty

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I need help with getting the results from one userform to transter to the next empty textbox on another userform when cmdbtn1 is clicked.

I have two userforms, both are always open when this task is needed. Userform1 has , in order, textbox1,2,3,4,5,6,8,9,10,11 (this is not a 7). userform2 has textbox13,12,11,10,9,8 which is displaying results some equations. I want the results from textbox13,12,11,10,9,8 to be displayed in textbox1,2,3,4,5,6,8,9,10,11 when cmdbtn1 is click on userform2.
 
Hello
See if this example is useful:

Code:
' this goes at a regular module
Sub TwoForms()
Dim ctl As Control
' load forms
UserForm3.Show vbModeless
With UserForm2
    .Show vbModeless
    .Top = 44
    .Left = 11
End With
For Each ctl In UserForm2.Controls
    If InStr(1, ctl.Name, "TextBox") > 0 Then ctl.Value = ctl.Name
Next
End Sub

Code:
' this goes at UserForm2 module
Private Sub CommandButton1_Click()
Dim orig, dest, i%, o_n$, d_n$
' this technique is handy when indexes are not sequential
orig = Array(1, 2, 3, 4, 5, 6)  ' text box indexes at source form
dest = Array(9, 8, 7, 6, 5, 4)  ' indexes at destination form
For i = LBound(orig) To UBound(orig)
    
    d_n = "TextBox" & dest(i)
    o_n = "TextBox" & orig(i)
    UserForm3.Controls(d_n).Value = UserForm2.Controls(o_n).Value
        
Next
End Sub
 
Upvote 0
I am not getting it to work properly. Everytime I initialize the userform the textboxes on userform2 are filled with TextBox1, TextBox2, TextBox3,.... Etc....Textbox11..... some of the textboxes one UserForm3 are also filling with "TextBox__" I can get more specific but first. What I would really like is Userform2 to be the only form to open when I click the cmdbtn on the spreadsheet and userform3 to open when a cmdbtn is click within Userform2... I know I said it differently in the beginnig but it's been requested to change.

so I guess the first code would look like this
Code:
Sub TwoForms()
Dim ctl As Control
' load forms

With UserForm2
    .Show vbModeless
    .Top = 44
    .Left = 11
End With
For Each ctl In UserForm2.Controls
    If InStr(1, ctl.Name, "TextBox") > 0 Then ctl.Value = ctl.Name
Next
End Sub

Then a cmd button on Userform2 would initialize Userform3 like this
Code:
UserForm3.show vbModeless
(This is how it is currently setup)
 
Upvote 0
Please test this:

Code:
' this goes at the sheet module
Private Sub CommandButton1_Click()
    
    UserForm2.Show vbModeless
    
End Sub

Code:
' this goes at UserForm2 module
Private Sub CommandButton1_Click()
Dim orig, dest, i%, o_n$, d_n$, ctl As Control
' this technique is handy when indexes are not sequential
orig = Array(1, 2, 3, 4, 5, 6)  ' text box indexes at source form
dest = Array(9, 8, 7, 6, 5, 4)  ' indexes at destination form
With UserForm3
    .Show vbModeless
    .Top = 12
    .Left = 21
End With
For i = LBound(orig) To UBound(orig)    ' transfer data to UserForm3
    
    d_n = "TextBox" & dest(i)
    o_n = "TextBox" & orig(i)
    Set ctl = UserForm3.Controls(d_n)
    ctl.Value = "This is " & ctl.Name & " at " & ctl.Parent.Name _
     & " with the text: " & vbNewLine & UserForm2.Controls(o_n).Value
Next
End Sub
Private Sub UserForm_Initialize()
Dim ctl As Control
' fill text boxes with some data
For Each ctl In UserForm2.Controls
    If InStr(1, ctl.Name, "TextBox") > 0 Then
        ctl.Value = ctl.Name & " at " & Me.Name
        ctl.MultiLine = True
    End If
Next
End Sub

Code:
' this goes at UserForm3 module
Private Sub UserForm_Initialize()
Dim ctl As Control
For Each ctl In Me.Controls
    
    If InStr(1, ctl.Name, "TextBox") > 0 Then ctl.MultiLine = True
    
Next
End Sub
 
Upvote 0

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