set focus in userform not working

mattrx731

Board Regular
Joined
Feb 10, 2008
Messages
181
I'm using the following code in a userform, and I'd like the cursor to go back to the textbox named 'textbillingzip' if there is an error... which I thought it would do with the line
Code:
Me.Textbillingzip.SetFocus
Instead it just continues to the next object in the tab order.

Code:
'fills in zip codes
Private Sub Textbillingzip_exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim rng As Range
Set rng = Sheets("lists").Columns(25).Find(Textbillingzip.value)

If Len(Textbillingzip.value) <> 5 Then
   Me.Textbillingzip.SetFocus
   MsgBox "Zipcode must be 5 numbers long", vbCritical, "Zip Code"
   Exit Sub
End If
If Not rng Is Nothing Then
    Label42 = rng.Offset(, 1).value
    Label43 = rng.Offset(, 2).value
Else
    Me.Textbillingzip.SetFocus
    MsgBox "Zip Code NOT found", vbExclamation, "Zip Code"
End If
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
try some thing like this.

Code:
With UserForm1.TextBox1
  .SelStart = 0
  .SelLength = Len(.Text)
  .SetFocus
 End With
 
Upvote 0
Isn't that what I have done with the line???
Code:
Me.Textbillingzip.SetFocus

If not, please show me where
Code:
With UserForm1.TextBox1
  .SelStart = 0
  .SelLength = Len(.Text)
  .SetFocus
 End With

would go in this code
Code:
'fills in zip codes
Private Sub Textbillingzip_exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim rng As Range
Set rng = Sheets("lists").Columns(25).Find(Textbillingzip.value)

If Len(Textbillingzip.value) <> 5 Then
   Me.Textbillingzip.SetFocus
   MsgBox "Zipcode must be 5 numbers long", vbCritical, "Zip Code"
   Exit Sub
End If
If Not rng Is Nothing Then
    Label42 = rng.Offset(, 1).value
    Label43 = rng.Offset(, 2).value
Else
    Me.Textbillingzip.SetFocus
    MsgBox "Zip Code NOT found", vbExclamation, "Zip Code"
End If
End Sub
 
Upvote 0
is there more to this code i dont see? were do you name the userform?
Code:
'the name of your userform
With UserForm1.Textbillingzip
.SetFocus
End With
 
End Sub
 
Upvote 0
The userform is named, but I'm just using Me. instead of the userform name every time. The name of the userform is 'formentry'
But I'm not sure you're trying to get the focus set when the userform opens, I just want it to happen if there is an error with this Textbillingzip_exit event.
 
Upvote 0
HMM , will sreenupdating will make it like the userform just open or on error your textbox.

Code:
On Error GoTo ErrorHdl
 
ErrorHdl:
 Application.ScreenUpdating = True
' or MY Be
ErrorHdl:
 Me.Textbillingzip.SetFocus
 
Upvote 0
The missing piece of the puzzle is just setting the CANCEL argument to TRUE, i.e.
Code:
Private Sub Textbillingzip_exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim rng As Range
Set rng = Sheets("lists").Columns(25).Find(Textbillingzip.value)
If Len(Textbillingzip.value) <> 5 Then
   MsgBox "Zipcode must be 5 numbers long", vbCritical, "Zip Code"
   cancel = true
   Exit Sub
End If
If Not rng Is Nothing Then
    Label42 = rng.Offset(, 1).value
    Label43 = rng.Offset(, 2).value
Else
    MsgBox "Zip Code NOT found", vbExclamation, "Zip Code"
    cancel = true
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,537
Messages
6,114,216
Members
448,554
Latest member
Gleisner2

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