+ Reply to Thread
Results 1 to 3 of 3

Validating data in a user form

Hybrid View

  1. #1
    Registered User
    Join Date
    03-26-2008
    Posts
    45

    Validating data in a user form

    Is there a way to validate data typed into the user form? I tried validating the actual cells that the data adds to, and that didn't work.
    For instance, I have a drop-down list now, but the way it is I can either choose from the list, or type in anything I want. What if I only want values from the list to be allowed? Is there a way to do such a thing?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Kim_J,

    Here is the code to restrict the user's input to the list in your ComboBox. The example uses a UserForm, a ComboBox, and a CommandButton. You can change the names and the loading of the ComboBox to match your needs. The code disables the user from using the Close Box on the title bar. You must close your form using a Command Button.
    Private Sub CommandButton1_Click()
     'CLOSE THE FORM
      Unload Me
      
    End Sub
    
    Private Sub UserForm_Activate()
     'VALIDATE THE ENTRY
       
       'The user's entry must match what is in the list
       'before can be passed to another object on the form.
        ComboBox1.MatchRequired = True
      
       'Load some data into the ComboBox
        ComboBox1.List = Array("Apples", "Grapes", "Kiwi", "Oranges", "Persimmons")
        
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
     'DISABLE THE CLOSE BOX
      If CloseMode = 0 Then Cancel = True
      
    End Sub
    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    03-26-2008
    Posts
    45
    Thank you so much for the reply. That helps a lot.

    How would I validate data that wasn't from a list? Like if it needed to be a date, or a certain text length?

    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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