Userform edit multiple textboxes at the same time

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
Is it possible to write to multiple textboxes at the same time.

For example:
1. Select first text box (say TextBox2)
2. Something like: Hold "Shift" and select last text box (say TextBox7)
3. Write Number, say 9


1728519619184.png
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Something like: Hold "Shift"

Maybe something like this.

Create a checkbox for each textbox, from 1 to 8, each check with its corresponding textbox
1728522447465.png

Mark the checkboxes corresponding to the textboxes you want to fill (They can be consecutive or not):
1728522544312.png

Enter number in the first textbox
1728522632781.png

Press Enter:
1728522685863.png

Put all the following code in your userform:
VBA Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(1)
End Sub
Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(2)
End Sub
Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(3)
End Sub
Private Sub TextBox4_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(4)
End Sub
Private Sub TextBox5_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(5)
End Sub
Private Sub TextBox6_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(6)
End Sub
Private Sub TextBox7_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(7)
End Sub
Private Sub TextBox8_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then Call filltextboxes(8)
End Sub

Sub filltextboxes(n As Long)
  Dim i As Long
 
  For i = 1 To 8
    If Controls("CheckBox" & i) = True Then
      Controls("TextBox" & i).Value = Controls("TextBox" & n).Value
      Controls("CheckBox" & i) = False
    End If
  Next
End Sub

Note: If you have more than 8 textboxes (how many?), then we could create a class, so as not to repeat the same code for each textbox.

😅
 
Upvote 0
Thanks DanteAmor, this is getting somewhere.
Yes there will be more than 8 textboxes, so will need to look at a class module.
Also I had to click each check box, was there a way to check first, press Shift, check next, and all in between are checked.
 
Upvote 0
If you have more than 8 textboxes (how many?)
Did you try the proposal?

Could you answer the questions.

-------------------------
was there a way to check first, press Shift, check next, and all in between are checked

As such there is no shift in the userform to mark the first and last.

I make the following proposal to you.
Checkboxes are no longer necessary.
1. Double click on the first textbox. Automatically it changes to green:
1728556395262.png

2. Double click on the next textbox. Automatically the middle textbox changes to green:

1728556505517.png

3. Capture the number in any green textbox and press enter. They are automatically filled in with the captured number:
1728556593485.png
Even if you repeat steps 1 and 2 on the textboxes that have numbers and delete the data in one of the textboxes, the data in all the green textboxes is automatically deleted, as if you were resetting to the initial values ;)

---------------
Put all the following code in the userform:
VBA Code:
Dim TxtBx() As New Class1         'At the start of all code
Public tot As Long

Private Sub UserForm_Initialize()
  Dim ctrl As MSForms.Control
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "TextBox" Then
      tot = tot + 1
      ReDim Preserve TxtBx(tot)
      Set TxtBx(tot).MultTextbox = ctrl
    End If
  Next
End Sub

Sub filltextboxes(n As Long)
  Dim i As Long
  
  For i = 1 To tot
    If Controls("TextBox" & i).BackColor = &H80FF80 Then
      Controls("TextBox" & i).Value = Controls("TextBox" & n).Value
      Controls("TextBox" & i).BackColor = &HFFFFFF
    End If
  Next
End Sub

Create a class module (Class1)
1728556979282.png
and put the following code:
VBA Code:
Public WithEvents MultTextbox As MSForms.TextBox

Private Sub MultTextbox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim i As Long, checkStart As Long, checkFinal As Long
  
  With UserForm1
    
    If MultTextbox.BackColor = &H80FF80 Then
      MultTextbox.BackColor = &HFFFFFF      'white
    Else
      MultTextbox.BackColor = &H80FF80      'green
    End If
    
    For i = 1 To .tot
      If .Controls("TextBox" & i).BackColor = &H80FF80 Then
        If checkStart = 0 Then
          checkStart = i
        ElseIf checkStart > 0 And checkStart < i Then
          checkFinal = i
        End If
      End If
    Next
    
    If checkStart > 0 And checkFinal > 0 Then
      For i = checkStart + 1 To checkFinal - 1
        .Controls("TextBox" & i).BackColor = &H80FF80
      Next
    End If
  End With

End Sub

Private Sub MultTextbox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  Dim num As Long
  If KeyCode = 13 Then
    num = Mid(MultTextbox.Name, 8, 3)
    Call UserForm1.filltextboxes(num)
  End If
End Sub

Try the proposal and tell me.
😇
 
Upvote 0
Solution
Thanks DanteAmor, this works and is very useful.

With the number of textboxes, the number is an unknown since they will come from a selected range which is different every time. The next step will be to get data from range and transfer to textboxes.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,655
Latest member
goranzoric

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