+ Reply to Thread
Results 1 to 4 of 4

Excel Userform - keep focus on textbox after error

Hybrid View

  1. #1
    Registered User
    Join Date
    11-29-2013
    Location
    Townsville, Australia
    MS-Off Ver
    Excel 2010
    Posts
    23

    Excel Userform - keep focus on textbox after error

    I have a userform where I am trying to check that a valid date is added into a textbox. If the entry is not a valid date, I want to clear the textbox, and give it the focus again, ready for the correct format date to be entered. I would also like to stop the user tabbing past the textbox without entering anything.
    Whatever I have tried has not worked - the focus goes to the next control. I feel it is because I am using the wrong event (but probably because I'm not very competent in VBA programming )
    My code is below...

    Private Sub txbACDateClosed_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
                If IsDate(txbACDateClosed) = False Then
                    MsgBox "You must enter a valid date...", vbOKOnly
                    txbACActionNumber.SetFocus
                    Exit Sub
    
                End If
    
    End Sub
    All and any help greatly appreciated.

    OM1950

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Excel Userform - keep focus on textbox after error

    It's much better validating all the values when the user tries to submit the form. It keeps things simple for you and stops your workbook pissing them off if they don't want to fill the form out in the same order that you want them to!

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Excel Userform - keep focus on textbox after error

    To keep focus on the textbox set Cancel = True.
    Private Sub txbACDateClosed_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
                If IsDate(txbACDateClosed) = False Then
                    MsgBox "You must enter a valid date...", vbOKOnly
                    Cancel = True
    
                End If
    
    End Sub
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    11-29-2013
    Location
    Townsville, Australia
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Excel Userform - keep focus on textbox after error

    Thanks Norie - works like a charm. I didn't want to check everything at the end of entry Kylie123, because it is possible for the user to leave the form for another userform that depends on some data in the initail one (sorry to be confusing here...) but thanks for the reply.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Userform Minimize Button Code Blocking Focus for First TextBox in Frame
    By excelforumkeys in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-14-2014, 12:57 PM
  2. Setting focus on a textbox in userform on Excel VBA
    By Shanthan in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-01-2013, 12:25 AM
  3. Set focus and select value on a userform textbox
    By Khaos176 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2013, 04:40 PM
  4. [SOLVED] UserForm TextBox validation and focus
    By Indiana Epilepsy and Child Neurology in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-14-2011, 03:10 AM
  5. ? set focus to a textbox when userform opens
    By madhg in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-30-2009, 04:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1