Tab order & text boxes

staticbob

Well-known Member
Joined
Oct 7, 2003
Messages
1,079
Guys,

Is it possible to control the tab order on a protected spreadsheet, into and out of text boxes ?!?

EG, I have 6 cells unlocked, and 3 text boxes. I need to control tab order if poss. At the moment, I need to click into and out of the text boxes.

Cheers
Bob
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
This worked for me:

Code:
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then
        TextBox2.Activate
    End If
End Sub

Private Sub TextBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then
        TextBox3.Activate
    End If
End Sub

Private Sub TextBox3_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then
        TextBox1.Activate
    End If
End Sub
 
Upvote 0
Thanks Andrew,

That will work fine once I'm in text box 1, is there a way to tab into the first text box ? I presume I need to put code in to identify the last cell in the tab order, then put similar code on selection_change ?

Hmmmmm.

Bob
 
Upvote 0
Try this:

Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then
        TextBox2.Activate
    End If
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then
        TextBox3.Activate
    End If
End Sub

Private Sub TextBox3_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 9 Then
        Range("A1").Activate
    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static OldCell As String
    If OldCell = "C2" Then
        TextBox1.Activate
    End If
    OldCell = Target.Address(False, False)
End Sub

Note that I changed to the KeyDown event. KeyUp was firing when I released the TAB key and I ended up in TextBox2.

I have assumed your 6 entry cells are in the range A1:C2. Change as needed.
 
Upvote 0
Thanks Andrew.

However my code looks similar to this anyway :

dim lastcell as string

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Select Case Target.Address
Case "$F$12"
lastcell = "F12"
Case "$D$39"
If lastcell = "F12" Then
TextBox1.Activate
End If
End
End Select

Textbox1 lies in the tab order between F12 and D39 so, when Im in F9 and hit the tab key the cursor moves to D39, works out that it should be in textbox1 and moves it there. The problem is that the focus is still at D39 although the cursor is in textbox1, offscreen.
 
Upvote 0
If the TextBox is not visible you need to scroll to its TopLeftCell:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static OldCell As String
    If OldCell = "C2" Then
        Application.Goto TextBox1.TopLeftCell, True
        TextBox1.Activate
    End If
    OldCell = Target.Address(False, False)
End Sub
 
Upvote 0
Thanks Andrew,

Works a treat now - to make things a bit neater I created a new, hidden textbox and use application.goto to scroll to that so that textbox1 comes up front and center.

Job done! Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,522
Messages
6,125,312
Members
449,218
Latest member
Excel Master

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