Select next TextBox if previous TextBox has a value

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I am using the code supplied below.
When i open my workbook the userform called DiscoForm opens.
There are 7 Textboxes but i only need to concentrate on 5 of them.

What i am trying to do is have the code select the next TextBox once a value is entered in the previous TextBox

So a user enters a value in TextBox 1 then have the code select TextBox2
The pattern follows etc Value in TextBox2 then select TextBox3 etc etc

The value will either be 1 letter or 1 number
I have started below but then hit a wall as the TextBox didnt get selected.
Hitting enter on keybord does select the next TextBox if that helps you.

Thanks



Rich (BB code):
Private Sub SendToWorksheet_Click()
    If TextBox1.Value <> 0 Then
      TextBox2.SetFocus Focus
      
    If TextBox2.Value <> 0 Then
      TextBox3.SetFocus Focus
      
    If TextBox3.Value <> 0 Then
      TextBox4.SetFocus Focus
      
    If TextBox4.Value <> 0 Then
      TextBox5.SetFocus Focus
    
    ThisWorkbook.Worksheets("Sheet1").Range("P6") = Me.TextBox1.Text
    ThisWorkbook.Worksheets("Sheet1").Range("Q6") = Me.TextBox2.Text
    ThisWorkbook.Worksheets("Sheet1").Range("R6") = Me.TextBox3.Text
    ThisWorkbook.Worksheets("Sheet1").Range("S6") = Me.TextBox4.Text
    ThisWorkbook.Worksheets("Sheet1").Range("T6") = Me.TextBox5.Text
    ThisWorkbook.Worksheets("Sheet1").Range("U6") = Me.TextBox6.Text
    ThisWorkbook.Worksheets("Sheet1").Range("P7") = Me.TextBox7.Text
    ActiveWorkbook.Save
    Application.ScreenUpdating = True

    Unload Me
    
Dim answer As Long
Dim currentShape As Shape

Sheets("Sheet1").Range("P6:U6").Copy Sheets("Sheet1").Range("E6")
Sheets("Sheet1").Range("P7").Copy Sheets("Sheet1").Range("E7")

Sheets("Sheet1").Range("E6:J6").Copy Sheets("PRINT LABELS").Range("E5")
Sheets("Sheet1").Range("E7").Copy Sheets("PRINT LABELS").Range("E6")

Sheets("Sheet1").Activate
ActiveSheet.Range("E7").Select

    Application.ScreenUpdating = False
    
   
    With Sheets("PRINT LABELS")
        .Range("M1").ClearContents
        For Each currentShape In .Shapes
            If currentShape.Type = msoPicture Then
                currentShape.Delete
            End If
        Next currentShape
    End With
    
    Application.ScreenUpdating = True
    
ActiveWorkbook.Save

With Sheets("PRINT LABELS")
.Activate
.Range("A1").Select
ActiveWorkbook.FollowHyperlink Address:="http://www.reeves.tv/mini.html"
End With
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi

try

VBA Code:
Private Sub SendToWorksheet_Click()
     Dim i As Long
    For i = 1 To 5
      With Me.Controls("TextBox" & i)
          If Len(.Value) = 0 Then .SetFocus: Exit Sub
      End With
    Next i
  
    'rest of code

Dave
 
Upvote 0
Hi,
After entering a value in TextBox1 i was expecting TextBox2 to then have the cursor flashing so i could just enter a value but TextBox1 was still active
 
Upvote 0
Hi,
After entering a value in TextBox1 i was expecting TextBox2 to then have the cursor flashing so i could just enter a value but TextBox1 was still active
I assumed that your code is commandbutton_Click event and that you wanted to check for completeness of all textboxes before code continues? If so, code should do this.

Dave
 
Upvote 0
Screenshot added.
At present i add a value in TextBox1 then i selected TextBox2 & add a value this continues......
I am trying to have vba select the next TextBox in the series for me once value has been entered
 

Attachments

  • 4786.jpg
    4786.jpg
    80.5 KB · Views: 7
Upvote 0
I am trying to have vba select the next TextBox in the series for me once value has been entered
A little confused as your code sample is for a click event but all should need to do once data is entered, just press the enter key should tab you to next textbox.

Dave
 
Upvote 0
I agree with what you have said & trying to get away with not having to press enter on the keyboard.

Some code should see that once a value is entered in the TextBox1 then move to TextBox2 otherwise wait for a value to be entered.
Maybe need to place the code elsewhere ?
 
Upvote 0
Maybe need to place the code elsewhere ?
Then you don't need code you would use the AutoTab property of the control

If you select each textbox & go to the properties window

Set the following properties

AutoTab = True

MaxLengh = (enter the maximum no required characters)

When you enter text in the control it should autotab WHEN entry = MaxLengh value

Dave
 
Upvote 0
Solution
Well never knew that wa there.
Many thanks that is what was required.

One to remember as it is very handy.

Have a nice day.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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