Excel VBA Check if Postal code falls within Postal Code Range

  • Hi there,


    I am working with a project where I need to lookup a value based on cell value in my data (a postal code, i.e. A0A 1A9), versus a lookup list that is organized by postal code ranges.


    Example, the value I am searching for is A0A 1A8 (assume this is in cell A1).


    My lookup table is organized as:


    [TABLE="width: 500, align: center"]

    [tr]


    [td][/td]


    [td]

    A0A1A0

    [/td]


    [td]

    A0A1A9

    [/td]


    [td]

    CODE A

    [/td]


    [/tr]


    [tr]


    [td][/td]


    [td]

    A0A1B0

    [/td]


    [td]

    A0A1B9

    [/td]


    [td]

    CODE B

    [/td]


    [/tr]


    [tr]


    [td][/td]


    [td]

    A0A1C0

    [/td]


    [td]

    A0A1E9

    [/td]


    [td]

    CODE C

    [/td]


    [/tr]


    [/TABLE]


    What I am after is being able to return the value in the 3rd column (i.e. "Code A").


    The code would need to understand that A0A1A8 falls within the range of (A0A1A0 to A0A1A9), and thus "CODE A" is the return value.


    As a second example, if I were to lookup "A0A1D3", the code would need to understand that it falls within the range (A0A1C0 to A0A1E9) because "D" comes before "E" alphabetically and this falls in this range.


    Note, the scope of the range is variable as you can see by the 3rd row in my "lookup table" as it spans across 2 alpha in the 5th character slot.


    Thanks all!

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Hi Jindon,


    Pretty close, except for range A0A1C0 to A0A1E9.


    If I search for A0A9C1 is returns a no match...


    Each character slot can be A-Z and 1-9 so the code A0A9C1 should return CODE C as it is in between A0A1C0 to A0A1E9.


    Hope I explained that ok?


    Thanks!

  • Re: Excel VBA Check if Postal code falls within Postal Code Range



    Does "range" means only between "C0" and "E9" and first 4 digits must be ignored?

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Unfortunately none of the digits can be ignored


    Example we could have a range of:
    A0A1A0 to B0B 1A0


    so this would encompass all of the postal codes with A0A as the first three digits, and then all of the combinations for the last 3 digits (i.e. 1A0 (1 could be 1-9), (A could be A-Z), (0 could be 0-9)). Then the firs three digits would change to A0B, and repeat the steps for the last three digits.


    Basically any alpha character can be a-z, any digit character can be 0-9, so the code would need to check all 6 digits, not just the last three, to be able to properly figure out if the value we are searching for falls within the range.

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Because the range would have went something like


    A0A 1C0 - then
    1C1
    1C2
    1C3
    1C4
    1C5
    1C6
    1C7
    1C8
    1C9
    THEN
    2C0 - and all the combos from 0-9 for the last digit
    3C0 - and all the combos from 0-9 for the last digit
    4C0 - and all the combos from 0-9 for the last digit
    etc.


    Once it gets to A0A 9C9 the "C" would then change to


    A0A1D0 etc.

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Jindon,


    Early tests suggest this is working perfectly. Will continue testing and let you know, but from what I can see...AWESOME work again!

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Hi Jindon,


    It looks like I am going to have to use this as a UDF (user defined function), so the mytxt would be grabbed from the udf input. Tried to revise your code, but getting errors, figured you might be able to understand quicker than I.


    thanks!

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Quote from powera86;618837

    Hi Jindon,


    It looks like I am going to have to use this as a UDF (user defined function), so the mytxt would be grabbed from the udf input. Tried to revise your code, but getting errors, figured you might be able to understand quicker than I.


    thanks!


    I guess so, but how did you try?

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    This is what I have, but am getting #VALUE error when UDF calculates:


  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    =GetCode(F2,A1:C3)


    Where F2 = PostCode to be examined, A1:C3 holds the table.


  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Hi Jindon,


    Looks like I ran into a bit of an issue so hoping that you can shed some light.


    The postal code I am "searching" is P8N2P4


    In my table there is a range P1A1A0 to P8N3L9, but when the code executes it isn't finding this range.


    Many many thanks!

  • Re: Excel VBA Check if Postal code falls within Postal Code Range



    Howcome P8N2P4 is in the range of P1A1A0 to P8N3L9?

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Hey Jindon,


    Brain fart, you are absolutely correct. Apologies for wasting your time. Must be Monday...and not enough coffee yet today!

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Hi Jindon,


    It looks like I am just losing it here...been a long week already. P8N2P4 should be in the range P1A1A0 to P8N3L9 because the P8N2P4 would come before P8N3L9 as the fourth character is less than 3. Does that make sense?

  • Re: Excel VBA Check if Postal code falls within Postal Code Range


    Make sense
    change to

    Code
    Function GetAsValue(ByVal txt As String) As Long
        If txt Like "[A-Z][0-9][A-Z][0-9][A-Z][0-9]" Then
            GetAsValue = Asc(Mid$(txt, 1, 1)) & Mid$(txt, 2, 1) _
            & Asc(Mid$(txt, 3, 1)) & Mid$(txt, 4, 1) & Asc(Mid$(txt, 5, 1)) & Mid$(txt, 6, 1)
        End If
    End Function

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!