+ Reply to Thread
Results 1 to 4 of 4

Excel 2007 : Data validation with numbers and text

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2012
    Location
    Anderson, South Carolina
    MS-Off Ver
    Excel 2007
    Posts
    2

    Data validation with numbers and text

    I need formulas for data validation to limit this information.

    1. XX00000
    2. 00000000X
    3. 000000X

    Where XX is any combination of letters (upper or lowercase) and
    00000 is a combination of any 5 numbers.

    All of these formaulas can not allow special characters.

    These can not be array formulas.

    It must always be in this format, with this number of digits in total on each

    By imputting a specific formula data validation this will only allow a user to type in this specific criteria.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Data validation with numbers and text

    Hi Madams, welcome to the forum.

    Rather than using a convoluted Data Validation formula, perhaps a worksheet change event (macro) would be better suited? This code, which would be added to the code module for the worksheet on which you're entering data, will check column A for any changes as you make them, and determine whether or not the entry is valid. It looks for the three formats you specified above (XX00000, 00000000X and 000000X). You can adjust the code to suit your actual data layout.
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Long
    If Target.Cells.Count > 1 Then Exit Sub
    
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        Select Case Len(Target.Value)
            Case Is = 7
                If Asc(UCase(Mid(Target.Value, 1, 1))) < 65 Or Asc(UCase(Mid(Target.Value, 1, 1))) > 90 Then GoTo invalid
                If Asc(UCase(Mid(Target.Value, 2, 1))) < 65 Or Asc(UCase(Mid(Target.Value, 2, 1))) > 90 Then GoTo invalid
                For i = 3 To 7
                    If Not IsNumeric(Mid(Target.Value, i, 1)) Then GoTo invalid
                Next i
            Case Is = 9
                For i = 1 To 8
                    If Not IsNumeric(Mid(Target.Value, i, 1)) Then GoTo invalid
                Next i
                If Asc(UCase(Mid(Target.Value, 9, 1))) < 65 Or Asc(UCase(Mid(Target.Value, 9, 1))) > 90 Then GoTo invalid
            Case Else
                GoTo invalid
        End Select
    End If
    Exit Sub
    
    invalid:
    MsgBox "Invalid Entry.  Entries must be in the form of XX00000, 00000000X or 000000X. Please try again."
    Application.EnableEvents = False
    Application.Undo
    Application.EnableEvents = True
    End Sub
    This could also be done using Regular Expressions (regex), but I'm not very good at using those just yet.

  3. #3
    Registered User
    Join Date
    06-19-2012
    Location
    Anderson, South Carolina
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Data validation with numbers and text

    where would I specify the specific cells within the macros. Each 1, 2, and 3 have different cells. Im not familiar with macro, I really appreciate all of your help.

  4. #4
    Registered User
    Join Date
    12-03-2012
    Location
    Greenville, SC
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Data validation with numbers and text

    I had an issue similar to this involving lot numbers in the format 0000XX. I had the user enter the numbers and letters in separate cells, and then concatenate (and capitalize) them after validation.

    For the numbers, I am validating for a whole number from 1-9999. The cell's custom format is 0000.

    For the letters, I used the following custom formula to validate that only two letters may be entered.
    =AND(LEN(A1)=2,ISNUMBER(FIND(LOWER(LEFT(A1)),"abcdefghijklmnopqrstuvwxyz")),ISNUMBER(FIND(LOWER(RIGHT(A1)),"abcdefghijklmnopqrstuvwxyz")))
    The formula was provided by circledchicken, on another forum.

+ 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