Move between TEXTBOX USERFORM left and right using the arrows

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello, I have a user form that contains several TEXTBOX. I want a code that enables you to move between them using the arrows left and right.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Put the following code in your userform:

VBA Code:
Dim TxtBx() As New Class1         'At the start of all code

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


In a class module put the following code:

VBA Code:
Public WithEvents MultTextbox As MSForms.TextBox

Private Sub MultTextbox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 39 Then SendKeys "{TAB}"
  If KeyCode = 37 Then SendKeys "+{TAB}"
End Sub


In the following link I explain how to create a class:

Regards
Dante Amor
 
Upvote 1
Solution
Thank you it works very well on 80 textbox, I have a simple question please. Is there a way to highlight the textbox when moving, meaning when moving the selected element will have a different background color and the rest will be white and so on
 
Upvote 1
Is there a way to highlight the textbox when moving

Update the class module with following:
Change UserForm1 to the name of your userform
Change this information: &HFF00& to the color you like.

VBA Code:
Public WithEvents MultTextbox As MSForms.TextBox

Private Sub MultTextbox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 39 Then SendKeys "{TAB}"
  If KeyCode = 37 Then SendKeys "+{TAB}"
End Sub

Private Sub MultTextbox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  Dim xd, ctrl As MSForms.Control
  xd = MultTextbox.Name
  With UserForm1
    For Each ctrl In .Controls
      ctrl.BackColor = &H80000005
    Next
    .Controls(xd).BackColor = &HFF00&
  End With
End Sub

😇
 
Upvote 0

Forum statistics

Threads
1,224,144
Messages
6,176,647
Members
452,739
Latest member
SCEducator

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