got a rather easy one, but i am not able to figure out meself.
i am using 2 text boxes and a command button (control, not form) on a
sheet.
i would like tab out to activate / focus on the next box and then the
command button, as and when data entry is perfomed and tab button is
hit.
One way is to do this with a bit of VBA code. Right click on the
sheet's tab and select "View Code". In the sheet's code module, paste
this in and make the appropriate name changes:
'-------------------------------------------------
'Change TextBox1 to the name of your text box.
'Do NOT change anything else in the two lines below.
Private Sub TextBox1_KeyUp(ByVal KeyCode _
As MSForms.ReturnInteger, ByVal Shift As Integer)
'Did the user just press the tab key?
If KeyCode = vbKeyTab Then
'If so, did they hold down the Shift key while they
'did it? If so, move back.
'Change the names shown to your own control names.
'"Me" refers to the worksheet holding the controls.
If Shift Then
Me.CommandButton1.Activate
Else
Me.TextBox2.Activate
End If
End If
End Sub
'-------------------------------------------------
'Change TextBox2 to the name of your text box.
'Do NOT change anything else in the two lines below.
Private Sub TextBox2_KeyUp(ByVal KeyCode _
As MSForms.ReturnInteger, ByVal Shift As Integer)
'Did the user just press the tab key?
If KeyCode = vbKeyTab Then
'If so, did they hold down the Shift key while they
'did it? If so, move back.
If Shift Then
Me.TextBox1.Activate
Else
Me.CommandButton1.Activate
End If
End If
End Sub
'-------------------------------------------------
Private Sub CommandButton1_KeyUp(ByVal KeyCode _
As MSForms.ReturnInteger, ByVal Shift As Integer)
'Did the user just press the tab key?
If KeyCode = vbKeyTab Then
'If so, did they hold down the Shift key while they
'did it? If so, move back.
If Shift Then
Me.TextBox2.Activate
Else
Me.TextBox1.Activate
End If
'Although this will ACTIVATE the command button, pressing
'Enter while you're on it won't run its code. So lets fix that.
ElseIf KeyCode = vbKeyReturn Then
CommandButton1_Click
End If
End Sub
'-------------------------------------------------
Private Sub CommandButton1_Click()
MsgBox "Hello World!"
End Sub