+ Reply to Thread
Results 1 to 9 of 9

Checking that a cell contains 4 numbers followed by 7 numbers

  1. #1
    Registered User
    Join Date
    05-08-2018
    Location
    Hazelton, Canada
    MS-Off Ver
    2013
    Posts
    19

    Post Checking that a cell contains 4 numbers followed by 7 numbers

    In my line of work we use sea-cans to transport a great deal of goods.
    The sea-cans are always tracked by an 11 digit code which is 4 letters followed by 7 numbers.
    Example: ABCD1234567

    When I am entering these sea-cans into our database, sometimes a number is missed, or an extra one is entered, etc.
    I'm looking to create some sort of highlight-cell function or IF function to return the cell red if it does not contain exactly 11 digits (4 letters followed by 7 numbers).

    Any help would be greatly appreciated!

    Regan

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,147

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    VBA solution

    Please Login or Register  to view this content.
    Right Click on tab where data is to be entered, "View Code" and copy/paste the above.

    Assumes data is column A: change highlighted to required column.

    file save with xlsm extension.
    Attached Files Attached Files

  3. #3
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.84 for Mac MS 365
    Posts
    8,496

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    conditional formatting alternative, formula =len(a1)<>11
    format as you want, for example red bold italic font
    format paint down and assumed it is in column A

    EDIT:
    AND, if it is only an issue of length.
    Last edited by Sam Capricci; 05-08-2018 at 04:43 PM.
    Make contributors happy, click on the "* Add Reputation" as a way to say thank you.
    Sam Capricci

  4. #4
    Registered User
    Join Date
    05-08-2018
    Location
    Hazelton, Canada
    MS-Off Ver
    2013
    Posts
    19

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    Awesome! Thanks so much
    and if I want it to target a single cell? (the Excel in my manifest template where I enter the can information is G7)
    Sorry still learning

  5. #5
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,147

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    Change:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-08-2018
    Location
    Hazelton, Canada
    MS-Off Ver
    2013
    Posts
    19

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    Will this work:

    Private Sub Worksheet_Change(ByVal Target As Range)

    'Do nothing if more than one cell is changed or content deleted

    If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub

    'Stop any possible runtime errors and halting code

    On Error Resume Next

    Application.EnableEvents = False

    If Target.Cell = G7 Then ' Check data entry is column G

    If Len(Target) <> 11 Then GoTo error_value ' Wrong length

    For i = 1 To 4
    If Asc(UCase(Mid(Target, i, 1))) < 65 Or Asc(UCase(Mid(Target, i, 1))) > 90 Then GoTo error_value 'Not alpha
    Next i

    If Not IsNumeric(Right(Target, 7) + 0) Then GoTo error_value ' not numeric

    Application.EnableEvents = True ' All OK
    Exit Sub


    error_value:

    MsgBox Target.Value & " is an invalid entry"

    Application.EnableEvents = True

    On Error GoTo 0

    End If




    End Sub
    Sub x()
    Application.EnableEvents = True
    End Sub

  7. #7
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,147

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    No

    Please Login or Register  to view this content.
    This ONLY checks G7.

  8. #8
    Registered User
    Join Date
    05-08-2018
    Location
    Hazelton, Canada
    MS-Off Ver
    2013
    Posts
    19

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    Perfect - that's what I'm after. Thanks a ton!!

  9. #9
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,147

    Re: Checking that a cell contains 4 numbers followed by 7 numbers

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. [SOLVED] Checking that a cell contains 4letters then 7 numbers
    By ReganK in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-27-2018, 04:07 PM
  2. checking numbers if they are identical
    By fric in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-29-2017, 08:46 AM
  3. Need help checking bingo numbers
    By kkaistha in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2015, 10:07 AM
  4. [SOLVED] Checking if 20 consecutive numbers in a raw are below a given value
    By Brool in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-21-2015, 07:39 AM
  5. Checking for numbers in string
    By ramserp in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 12-07-2009, 04:05 AM
  6. Checking crossword numbers
    By shanjar in forum Excel General
    Replies: 1
    Last Post: 04-07-2006, 05:15 PM
  7. [SOLVED] Checking numbers in range
    By Jan Kronsell in forum Excel General
    Replies: 14
    Last Post: 01-15-2006, 06:35 PM

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