+ Reply to Thread
Results 1 to 9 of 9

Validating account numbers using IF, OR, etc

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2007
    Location
    Leeds, England
    MS-Off Ver
    Professional Edition 2010
    Posts
    15

    Validating account numbers using IF, OR, etc

    Hi,

    I have a list of account numbers in the following variations:

    AAA123456ISA
    AAA123456ISAB
    AAA123456IF
    AAA123456IFB
    AAA123456SIPP
    AAA123456SIPPB
    BBB123456ISA
    BBB123456ISAB
    BBB123456IF
    BBB123456IFB
    BBB123456SIPP
    BBB123456SIPPB

    Basically, I need to be able to extract account numbers from my computer system and check that they have been set up in the correct format. Since there are thousands of account numbers I obviously wanted an automated process to do this. At the moment, I am using the following formula:

    =IF(OR(RIGHT(A2, 3)="ISA", RIGHT(A2, 4)="ISAB", RIGHT(A2, 2)="IF", RIGHT(A2, 3)="IFB", RIGHT(A2, 4)="SIPP", RIGHT(A2, 5)="SIPPB"), "", "ERROR")

    which is rather clumsy. It is basically checking the last 3,4, or 5 digits of the account number and returning an error message if an incorrect format has been input. However this doesn't check the prefix of the account number (AAA or BBB) or if there are any other errors, for example:

    AAA123456-ISA (note, the dash)

    Please can anyone help me with a more thorough and efficient way of validating these account numbers?

    Thanks

  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

    Re: Validating account numbers using IF, OR, etc

    Hello jaredino,

    Welcome to the Forum!

    The macro below will validate the account numbers based on your formula and if the prefix is three capital letters. There is button on the worksheet of the attached workbook to run the macro on a sample of the data you posted.

    Experience has taught me this macro will need to modified to work with your original. People seldom post real account numbers or provide a large enough sample for testing. Give it try and let me know the results.
    Sub CheckAccountNumbers()
    
    ' Thread:  http://www.excelforum.com/excel-programming/798347-validating-account-numbers-using-if-or-etc.html
    ' Poster:  jaredino
    ' Written: October 26, 2011
    ' Author:  Leith Ross
    
        Dim Cell As Range
        Dim RegExp As Object
        Dim Rng As Range
        Dim RngEnd As Range
        Dim Wks As Worksheet
        
            Set Wks = Worksheets("Sheet1")
            Set Rng = Wks.Range("A2")
            
                Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
                If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
                
                Rng.Offset(0, 1).ClearContents
                
                Set RegExp = CreateObject("VBScript.RegExp")
                RegExp.Pattern = "[A-Z]{3}\d+(IF[B]?|ISA[B]?|SIPP[B]?)$"
                
                For Each Cell In Rng
                    If RegExp.Test(Cell) = False Then Cell.Offset(0, 1) = "Invalid"
                Next Cell
                
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,794

    Re: Validating account numbers using IF, OR, etc

    Question, should not the line

    RegExp.Pattern = "[A-Z]{3}\d+(IF[b]?|ISA[b]?|SIPP[b]?)$"
    include IFB, ISAB and SIPPB ?

    Alf

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

    Re: Validating account numbers using IF, OR, etc

    Hello Alf,

    Here is how the pattern breaks down:

    [A-Z]{3} Must start with any combination of 3 capital letters.

    \d+ There must be 1 or more digits following the letters.

    (IF[b]?|ISA[b]?|SIPP[b]?)$
    This checks if the following combinations of letters follow the digits.
    IF, IFB, ISA, ISAB, ISPP, ISPPB

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Validating account numbers using IF, OR, etc

    You didn't specify exactly the requirements for the accountnumbers.
    So I deduced:

    Sub snb()
     sn = Columns(1).SpecialCells(2).Resize(, 2)
        
     For j = 1 To UBound(sn)
      sn(j, 2) = IIf(Left(sn(j, 1), 3) = UCase(Left(sn(j, 1), 3)) And Mid(sn(j, 1), 4, 6) = Format(Val(Mid(sn(j, 1), 4, 6)), "0") And InStr("|ISAB|IFB|SIPPB", Mid(sn(j, 1), 10)) > 0, "", "Err")
        Next
        
     Columns(1).SpecialCells(2).Resize(, 2) = sn
    End Sub



  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,794

    Re: Validating account numbers using IF, OR, etc

    Hi Leith

    Thanks for the break down of you macro.

    Alf

  7. #7
    Registered User
    Join Date
    07-12-2007
    Location
    Leeds, England
    MS-Off Ver
    Professional Edition 2010
    Posts
    15

    Re: Validating account numbers using IF, OR, etc

    Genius - thanks, that's great. The only thing it doesn't check for is if there are 6 numbers between the prefix and suffix. Is this also possible?

    Also I'd like to add other columns to my spreadsheet but at the moment the macro is affecting all columns - how do I just have it run on the account number column? I really need to learn how to use Macros!!

    Thanks again for you help.

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

    Re: Validating account numbers using IF, OR, etc

    Hello jaredino,

    It is an easy change to make. Only one line needs to modified. The line with the Regular Expression Pattern:
      ' Use this pattern if the account number must have 6 digits - no more, no less
        RegExp.Pattern = "[A-Z]{3}\d{6}(IF[b]?|ISA[b]?|SIPP[b]?)$"
    
      ' Use this pattern if the account number must have a minimum and maximum number of digits
      ' This example is for 3 to 6 digits.
        RegExp.Pattern = "[A-Z]{3}\d{3,6}(IF[b]?|ISA[b]?|SIPP[b]?)$"

  9. #9
    Registered User
    Join Date
    07-12-2007
    Location
    Leeds, England
    MS-Off Ver
    Professional Edition 2010
    Posts
    15

    Re: Validating account numbers using IF, OR, etc

    Thanks, that's great!

+ 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