on tab out

  • Thread starter Thread starter icestationzbra
  • Start date Start date
I

icestationzbra

hi,

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
sheet.

i would like tab out to activate / focus on the next box and then th
command button, as and when data entry is perfomed and tab button i
hit.

any ideas?

thanks,

mac
 
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
 
hi,

thanks for the code. it worked fine.

i have another simple query. i tried to look it up in help, but could
not find it.

how do i capture ENTER-key-press?

i would like the macro to perform certain procedure when enter key is
pressed while focus is on a command button.

thanks,

mac.
 
hi hank,

i am really sorry, i missed reading the last part of you code where in
it mentioned how to deal with enter key.

it works completely, thank you very much.

mac.
 
Back
Top