+ Reply to Thread
Results 1 to 21 of 21

How do I extract a 5 digit zip code from a column that may have a random number of text?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-10-2016
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    How do I extract a 5 digit zip code from a column that may have a random number of text?

    Hi all,

    I have a spread sheet with a column that includes a random amount of text like so:

    -----------------------


    Contact Name:


    Contact Email:


    Contact Phone:

    I am interested in 1234NE 12Tth, Williston, FL 32696.


    Search CriteriaCity :
    State :
    ZIP : 0
    MinLotSize : 0
    MaxLotSize : 0
    MinYearBuilt : 0
    MaxYearBuilt : 0
    PropertyTypes : CondoApartment, LotsLand, Manufactured, MultiFamily, SingleFamily

    -------------------

    What I want to do: Extract only the zip code from this column.
    My problem: I cannot find a formula to complete this task because every cell is different. Some may display the above posted or some may have double the text, so I am not sure how to pinpoint the formula to do what I want it to do.

    The words "I am interested in" appear in a majority of cells in the column, followed by the respective state, if that helps.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    Assuming that your data is in column A and that the word "ZIP" is somewhere in that column, try the following macro:
    Sub FindZip()
        Application.ScreenUpdating = False
        Dim foundZip As Range
        Dim SplitZip As Variant
        Set foundZip = Range("A:A").Find("ZIP", LookIn:=xlValues, lookat:=xlPart)
        If Not foundZip Is Nothing Then
            SplitZip = Split(foundZip, ":")
            MsgBox SplitZip(1)
        End If
        Application.ScreenUpdating = True
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    05-10-2016
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    I am brand new with macros, so I am not entirely sure how to run this.

    I selected macros on the upper left, then created the macro by copying and pasting your code. Since the zip codes are in column D, I changed A to D instead. After I save the macro file, I am not too sure how to run and test it.

    I would ultimately want the zip codes inputted to a brand new column on the right.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    Hi, welcome to the forum

    It would probably be a lot easier to help if you could upload a small (clean) sample workbook (not a pic) of what you are working with, and what your expected outcome would look like.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Registered User
    Join Date
    05-10-2016
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    Thanks for the tip.

    So attached is a sample workbook of what I'll be working on. As you can see under the "notes" column, there is a body of text with the zip code. I want to be able to extract only the zip code shown to the row over to the right (column E)

    Thanks to everybody for the help so far.
    Attached Files Attached Files

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    I had a look at your file but I didn't see the word "ZIP" or any zip codes in column D. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the menu at the top click 'Insert' and then click 'Module'. Copy and paste the macro into the empty code window that opens up. Press the F5 key to run the macro. Close the code module window to return to your sheet. There are other quicker ways to run the macro such as assigning it to a button that you would click on your sheet or assigning it to a short cut key. Use this revised macro:
    Sub FindZip()
        Application.ScreenUpdating = False
        Dim foundZip As Range
        Dim SplitZip As Variant
        Set foundZip = Range("D:D").Find("ZIP", LookIn:=xlValues, lookat:=xlPart)
        If Not foundZip Is Nothing Then
            SplitZip = Split(foundZip, ":")
            foundZip.Offset(0, 1) = SplitZip(1)
        End If
        Application.ScreenUpdating = True
    End Sub
    If it doesn't work the way you expect it, then please have another look at your file to make sure that the zip codes are included and re-post it.

  7. #7
    Registered User
    Join Date
    05-10-2016
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    Thank you for the help, I followed the instructions provided but it did not fill only the zip code in column E. Instead, it seems like it copied and pasted some text from column D to E.
    Attached Images Attached Images

  8. #8
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,408

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    As both examples with zip codes relate to Florida, you could put this in E2:

    =IFERROR(MID(D2,FIND("FL",D2)+3,5),"")

    then copy down.

    Hope this helps.

    Pete

  9. #9
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    My apologies. Given the way that the data is organized, the macro I suggested won't work the way you want.

  10. #10
    Registered User
    Join Date
    05-10-2016
    Location
    United States
    MS-Off Ver
    2013
    Posts
    12

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    Hey, this worked! Thank you so much. So for different states, i would just have to replace "FL" with the correct one, right? Since the zip codes may be random, I would still need a useful macro to execute all zip codes, no matter the location.

  11. #11
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,408

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    There were only two examples (from three records) to work from. Both of them had FL followed by a space followed by the 5 digits that you wanted to extract. If you wanted something more generalised, you will need to submit another attachment which gives some more examples of the data that you have.

    Pete

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

    Re: How do I extract a 5 digit zip code from a column that may have a random number of tex

    Here's an UDF solution.
    Use in cell like
    =GetUSZipCode(D2)
    Function GetUSZipCode(ByVal txt As String) As String
        With CreateObject("VBScript.RegExp")
            .Pattern = "\b(A[LKZR]|C[AOT]|DE|FL|GA|HI|I[ADLN]|K[SY]|LA|M[AEDINSOT]" & _
            "|N[EVHJMYCD]|O[HRK]|PA|RI|S[CD]|T[NX]|[UT]V|VA|W[AVIY])[- ,]*(\d{5})\b"
            If .test(txt) Then GetUSZipCode = .Execute(txt)(0).submatches(1)
        End With
    End Function
    Attached Files Attached Files

+ 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. Replies: 3
    Last Post: 03-19-2016, 01:11 AM
  2. Replies: 6
    Last Post: 08-31-2015, 03:08 PM
  3. [SOLVED] 5-digit Random Unique Alphanumeric Code
    By mcodden in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-15-2015, 04:32 PM
  4. [SOLVED] 5-digit Random Unique Alphanumeric Code
    By mcodden in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-15-2015, 02:39 PM
  5. [SOLVED] extract 10 digit number only from text string
    By visha_1984 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 02-06-2015, 09:42 AM
  6. [SOLVED] VBA to convert cell to text and ensuring 7 digit number all along in entire column.
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-19-2013, 09:35 AM
  7. [SOLVED] Random 8 digit number?
    By KennyS in forum Excel General
    Replies: 6
    Last Post: 08-21-2006, 01:30 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