+ Reply to Thread
Results 1 to 12 of 12

Issue with Validating a Canadian Postal Code in excel 2016

  1. #1
    Registered User
    Join Date
    06-14-2018
    Location
    toronto
    MS-Off Ver
    2016
    Posts
    5

    Exclamation Issue with Validating a Canadian Postal Code in excel 2016

    Hello All,

    I am trying to validate a Canadian postal code in excel code. I am able to validate the code for the letters and numbers, example: e3r4t5. However I am trouble in creating a formula that is short enough to add a space between the e3r and 4t5.

    my code is: =(LEFT(K21)>="A")*(LEFT(K21)<="Z")*(MID(K21,2,1)>="0")*(MID(K21,2,1)<="9")*( MID(K21,3,1)>="A")*(MID(K21,3,1)<="Z")*(MID(K21,4,1)>="0")*(MID(K21,4,1)<="9 ")*(MID(K21,5,1)>="A")*(MID(K21,5,1)<="Z")*(MID(K21,6,1)>="0")*(MID(K21,6,1) <="9")*(LEN(K21)=6)

    Is there anyway to shorten this code to add a space between the third and fourth character?

    Thank you.

  2. #2
    Registered User
    Join Date
    06-04-2017
    Location
    India
    MS-Off Ver
    2013
    Posts
    54

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    If you want to add a space between 3rd and 4th character always, go to data->text to column and choose fixed width and then drag the pointer between 3rd and 4th character and done
    =concatenate(col1,col2," ")

    Also, your code is not adding any space .It is simply evaluating to TRUE OR FALSE

  3. #3
    Registered User
    Join Date
    06-13-2018
    Location
    india
    MS-Off Ver
    2010;2007
    Posts
    54

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    you could use
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    to add space between 3rd and 4th character




    if this helps then click add rep button at the bottom left corner of this post!

  4. #4
    Valued Forum Contributor jtakw's Avatar
    Join Date
    05-05-2018
    Location
    CA, USA
    MS-Off Ver
    2016
    Posts
    668

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    Hi,

    Assuming data in A1, you can also use this: =REPLACE(A1,4,0," ")

    This will Add a space after the 3rd character of your text string.

  5. #5
    Registered User
    Join Date
    06-14-2018
    Location
    toronto
    MS-Off Ver
    2016
    Posts
    5

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    sorry for not explaining this properly: is there any way in the data validation code to add the check for a space between the third and fourth character

    Thank you

  6. #6
    Registered User
    Join Date
    06-13-2018
    Location
    india
    MS-Off Ver
    2010;2007
    Posts
    54

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    you can use conditional formatting instead select the range of cells(say A1 to A100) you want to apply
    go to home tab ---> conditional formatting--->new rule--->use a formula to determine which cell top format
    copy paste
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    change bold part according to your need
    then select format select colour as red
    click ok

    find attached file its applied in A1 to A100

    if this helps click add rep button at the bottom left corner of this post
    Attached Files Attached Files

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,880

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    Try following if you want all following conditions met.

    1. Character positions 1, 3, 6 must be alphabet (A-Z or a-z).
    2. Position 4 must be space
    3. Positions 2, 5, 7 must be numeric
    4. Total length is 7 characters including space.

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  8. #8
    Registered User
    Join Date
    06-14-2018
    Location
    toronto
    MS-Off Ver
    2016
    Posts
    5

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    Thank you everyone for your help with solving my problem.

  9. #9
    Registered User
    Join Date
    06-14-2018
    Location
    toronto
    MS-Off Ver
    2016
    Posts
    5

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    Thanks for your help. Quick question about your formula: Why did you use (CHAR(ROW($A$65:$A$90) in your formula. Thank you.

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,880

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    You are welcome and thanks for the rep

    Basically, letters A to Z are expressed as Char(65) to Char(90).

    Formulas in Data validation are naturally evaluated as array formula.
    So CHAR(ROW($A$65:$A$90)) (i.e. CHAR(65 to 90)) will evaluate to {A,B,C,D,E...Z}. And = operator in formula is case insensitive.

    If your question is resolved, please mark the thread as solved by using Thread tool found at top of your initial post.

  11. #11
    Registered User
    Join Date
    06-14-2018
    Location
    toronto
    MS-Off Ver
    2016
    Posts
    5

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    Thank you very much.

  12. #12
    Registered User
    Join Date
    12-14-2021
    Location
    Florida
    MS-Off Ver
    MS Office 365
    Posts
    1

    Re: Issue with Validating a Canadian Postal Code in excel 2016

    I know I'm late to the party, here, but I took the OP's validation formula and simply added a section to the middle area to validate a space in the middle (4th position) and then adjusted the remaining positional numbers, as well as the length, accordingly.

    So, the OP's validation formula, as adjusted above, becomes:

    =(LEFT(K21)>="A")*(LEFT(K21)<="Z")*(MID(K21,2,1)>="0")*(MID(K21,2,1)<="9")*( MID(K21,3,1)>="A")*(MID(K21,3,1)<="Z")*MID(K21,4,1)=" "*(MID(K21,5,1)>="0")*(MID(K21,5,1)<="9 ")*(MID(K21,6,1)>="A")*(MID(K21,6,1)<="Z")*(MID(K21,7,1)>="0")*(MID(K21,7,1) <="9")*(LEN(K21)=7)

    which works in my spreadsheet.

    Mine actually referenced cell H2 rather than K21. I struggled a bit with the length of the formula, since, as typed above, it exceeded the maximum length for validation formulas; however, each occurrence of "K21" being replaced with my "H2" seemed to make it short enough. When I added new rows to my table to hit double digits (H10, etc.), it still worked.

    I hope this helps someone else who's struggling with this same issue here at the latter end of 2023 deal with that issue!

+ 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. Format Canadian Postal Code on Entry
    By Gos-C in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 03-24-2019, 04:20 PM
  2. Canadian postal code format
    By SLN in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 09-15-2016, 08:25 AM
  3. Canadian Postal Code format
    By nmurphy2 in forum Office 365
    Replies: 4
    Last Post: 10-02-2014, 01:01 AM
  4. [SOLVED] Userform for canadian postal code validation?
    By calvinle in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-01-2014, 01:03 PM
  5. [SOLVED] Help With Canadian Postal Code In Userform
    By eddyrcabrera79 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-04-2013, 08:16 PM
  6. Adding spaces to Canadian postal code
    By loren.silverman in forum Excel General
    Replies: 3
    Last Post: 03-01-2012, 10:02 PM
  7. Adding Canadian Postal Codes to Excel 07
    By bandaid8 in forum Excel General
    Replies: 5
    Last Post: 05-22-2009, 01:58 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