Setfocus not focusing

Matt0110

New Member
Joined
Feb 21, 2008
Messages
45
Hi all,

Why isn't the code below focusing on my textbox? I get the message box but when i click OK the focus is not set on my textbox where the wrong data has been entered.

Code:
Private Sub TextBox12_AfterUpdate()
If TextBox12.Enabled And TextBox12.Value < TextBox11.Value Then
 
    MsgBox "Please check reading - it appears to be lower than the first reading."
    TextBox12.SetFocus
End If
End Sub

Any help would be appreciated.

Cheers,
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
It's easier to use the Before_Update event:
Code:
Private Sub TextBox12_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
   If TextBox12.Enabled And TextBox12.Value < TextBox11.Value Then
    
       MsgBox "Please check reading - it appears to be lower than the first reading."
       Cancel = True
   End If
End Sub
HTH
 
Upvote 0
I should have mentioned that the reason the SetFocus appears not to work is that you haven't actually left the textbox when the event is triggered. Therefore your code resets the focus (no change) and then when you exit the routine, the cursor leaves the textbox and goes to the next control as normal.
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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