+ Reply to Thread
Results 1 to 24 of 24

How to check postal code format for user form text box

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    How to check postal code format for user form text box

    I'm hoping someone can help! I'm trying to create a data entry form using a userform. I've created a text box for Postal Code. In Canada, the format is A1A 1A1. I would like to validate that the person is entering the postal code in the right format when they are using the user form. I've stumbled on how to do it within the worksheet source code (http://www.excelforum.com/excel-prog...-on-entry.html), but there must be a way to do it when I initialize the user form? I'm using Excel 2010.
    Any help??

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    Use it as a function to evaluate valid or invalid.
    Function IsValidPostCode(ByVal txt As String) As Boolean
        IsValidPostCode = Replace(txt, " ", "") Like "[A-Z][0-9][A-Z][A-Z][0-9][A-Z]"
    End Function

  3. #3
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Sorry, you've lost me.... I'm new to this...

    My txt box is call txt_pc, how do I incorporate this with what you've given me!

    Thanks a bunch for replying!

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    Something like this
    To A UserForm module
    Private Sub txt_pc_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        With Me.txt_pc
            If Len(.Value) Then
                If Not IsValidPostCode(.Value) Then
                    MsgBox "Invalid post code", , .Value
                    Cancel = True
                End If
            End If
        End With
    End Sub
    
    Function IsValidPostCode(ByVal txt As String) As Boolean
        IsValidPostCode = Replace(txt, " ", "") Like "[A-Z][0-9][A-Z][A-Z][0-9][A-Z]"
    End Function

  5. #5
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Thanks Jindon!

    I was trying to load this checking before copying the data over when initializing using at button.

    So far, this is a sample of what I have:
    Private Sub CommandButton1_Click()
     
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Template")
    
    
    'find first empty row in database
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    
    'Validate Postal Code Format
    
    
    'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.txt_PC.Value
    
    
    'clear the data
    Me.txt_PC.Value = ""
    
    
    End Sub
    Can I insert your code above to where I have 'Validate Postal Code?
    Or, can you tell me more about how to use it as a userform module?

    Thanks again!
    Last edited by vlady; 03-11-2013 at 01:50 AM.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    1) You need to edit your last post and wrap the vba code with the code tag.
    It's a MUST rule here.
    i.e
    [code]
    vba code here
    [/code]

    2) Not tested.
    Private Sub CommandButton1_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    If Len(txt_pc.Value) Then
        If Not IsValidPostCode(txt_pc.Value) Then
            MsgBox "Invalid entry"
            Exit Sub
        End If
    End if
    Set ws = Worksheets("Template")
    'find first empty row in database
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    'Validate Postal Code Format
    'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.txt_PC.Value
    'clear the data
    Me.txt_PC.Value = ""
    End Sub
    
    Function IsValidPostCode(ByVal txt As String) As Boolean
        IsValidPostCode = Replace(txt, " ", "") Like "[A-Z][0-9][A-Z][A-Z][0-9][A-Z]"
    End Function

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    Quote Originally Posted by jindon View Post
    1) You need to edit your last post and wrap the vba code with the code tag.
    It's a MUST rule here.
    i.e
    [code]
    vba code here
    [/code]
    Post your workbook.

  8. #8
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Book1.xlsm

    Thanks!

  9. #9
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Thanks Jindon.

    I tried the code above, but it seems to not recognized the function IsValidPostCode. It allows me to enter the Postal Code, but it doesn't matter what I enter, it keeps coming up Invalid Entry. I'm sure it's simple, it seems so close!!!!

    Any suggestions?

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    ...................
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Hi Jindon,

    The xlsm file that you attached does not seem to work either. When I enter the postal code with the UserForm button in any format, it all comes back with invalid entry. The correct format should be A1A 1A1, but even when I enter that, it comes up as unvalid? Maybe I am doing something wrong?

    Thanks again!

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    A1a 1a1.......
    Attached Files Attached Files
    Last edited by jindon; 03-11-2013 at 02:01 AM. Reason: Password removed

  13. #13
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    That's great Jindon! Can I see the code so that I can learn what you did?

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    Nope.

    You must edit your post #5 first.

  15. #15
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Sorry for my ignorance, I don't know what that means. As I said, I'm new to this, so if you could explain, that would be great!

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    1) Click on [Edit Post].

    2) Select vba code in that post and click on "#" in the menu. It will add code tag automatically.

  17. #17
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    
    Private Sub CommandButton1_Click()
    
     Dim iRow As Long
     Dim ws As Worksheet
     Set ws = Worksheets("Template")
     
    
    'find first empty row in database
     iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
     SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
     
    'Validate Postal Code Format
     
    
    'copy the data to the database
     ws.Cells(iRow, 1).Value = Me.txt_PC.Value
     
    
    'clear the data
     Me.txt_PC.Value = ""
     
    
    End Sub

  18. #18
    Forum Moderator vlady's Avatar
    Join Date
    09-22-2011
    Location
    Philippines - OLSHCO -Guimba-Nueva Ecija
    MS-Off Ver
    2021
    Posts
    4,366

    Re: How to check postal code format for user form text box

    @ cpost, when posting codes. select the code then hit the "#" symbol to wrap it with code tags, I've done it for you but be sure to do it yourself next time.
    I think people forget the word "THANK YOU!!!!" Do you still know it???

    There is a little star ( ADD REPUTATION ) below those person who helped you. Click it to say your "PRIVATE APPRECIATION TO THEIR EFFORT ON THEIR CONTRIBUTIONS "

    Regards,
    Vladimir

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    Password removed.

  20. #20
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Thanks again Jindon! One more thing, any idea how I can enforce uppercase letters?

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    Add this to a Form module
    Private Sub txt_PC_Change()
        With Me.txt_PC
            If Len(.Value) Then .Value = UCase$(.Value)
        End With
    End Sub

  22. #22
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Hi Jindon,

    One last thing, if I want to enforce the space between, would I change the function to this:
    Function IsValidPostCode(ByVal txt As String) As Boolean
        IsValidPostCode = Replace(txt, " ", "") Like "[A-Za-z][0-9][A-Za-z][ ][0-9][A-Za-z][0-9]"
    End Function
    When I try this, it doesn't work?

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to check postal code format for user form text box

    Change
    ws.Cells(iRow, 1).Value = txt_PC.Value
    to
        ws.Cells(iRow, 1).Value = Left$(Replace$(Me.txt_PC.Value, " ", ""), 3) & _
            " " & Mid$(Replace$(Me.txt_PC.Value, " ", ""), 4)

  24. #24
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: How to check postal code format for user form text box

    Thank you so much Jindon for all your help and patience! I've learned so much! Hope others will see this as valuable as I did!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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