setfocus

bbb540kxn

New Member
Joined
Mar 9, 2011
Messages
2
I'm trying to highlight a userform text box if the user types in an error. I've used txtbox1.setfocus and txtbox1.selstart and sellength but after the message comes up it goes to next field. Below are the codes:

Private Sub TxtTeenAge_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If TxtTeenAge.Value = "" Then

ElseIf TxtTeenAge.Value < 7 Or TxtTeenAge.Value > 19 Then
TxtTeenAge.SelStart = 0
TxtTeenAge.SelLength = Len(TxtTeenAge.Text)
MsgBox ("less than or equal to 6")
End If

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try,
Code:
Private Sub TxtTeenAge_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    If TxtTeenAge.Value = "" Then

    ElseIf TxtTeenAge.Value < 7 Or TxtTeenAge.Value > 19 Then
        TxtTeenAge.SelStart = 0
        TxtTeenAge.SelLength = Len(TxtTeenAge.Text)
        [COLOR="Red"]Cancel = True[/COLOR]
        MsgBox ("less than or equal to 6")
    End If

End Sub
 
Upvote 0
Thanks a bunch - it works now. Been messing with this for awhile now. Is the code "cancel = true" overriding the tab index.

Thanks Again...
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,752
Members
452,940
Latest member
rootytrip

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