UserForm Set focus

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I’m stuck on a bit of VBA code that seems like it should be simple but everything I try is failing. I want to do an AfterUpdate event on a field where it checks if the data entered is a number, if it is then the code will format it as currency and if not it should pop-up with a warning to enter a number value, clear the field, and re-activate it. Everything is working except for re-activating the field, it keeps moving to the next one.

Code:
Private Sub Field1_AfterUpdate()



With Field1

If IsNumeric(.Value) = True Then

.Value = FormatCurrency(.Value, 2, vbTrue, vbTrue, vbTrue)

ElseIf .Value = "" Then

.Value = FormatCurrency(0, 2, vbTrue, vbTrue, vbTrue)

Else

MsgBox "Enter a number value"

.Value = ""

.SetFocus

End If

End With



End Sub
[code/]

thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
use BeforeUpdate and Cancel = True will keep you in the same textbox.
 
Upvote 0
Hi,
try using an event that has the cancel parameter like Exit event

VBA Code:
Private Sub Field1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   
    With Field1
       
        If IsNumeric(.Value) = True Or .Value = "" Then
           
            .Value = FormatCurrency(Val(.Value), 2, vbTrue, vbTrue, vbTrue)
           
        Else
           
            MsgBox "Enter a number value", 48, "Invalid Entry"
           
            .Value = ""
           
            Cancel = True
           
        End If
       
    End With
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

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