+ Reply to Thread
Results 1 to 14 of 14

Looking for a formula to extract a tax id from a string

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Looking for a formula to extract a tax id from a string

    Hello, I am looking for a formula to extract a numeric tax id (format 11-11111111) from a text string. The strings are very inconsistent, sometimes holding words and dash(es) before or after the tax id.

    Example data:
    11-11111111
    11-11111111
    CORP-11-11111111
    CORP--11-11111111
    CORP 11-11111111
    11-11111111 corp
    11-11111111-Corp
    -corp. 11-11111111
    no 1099 under $600 11-11111111
    11-11111111 no 1099 under $600
    no 1099 11-11111111 under $600

    Requested output from each line:
    11-11111111

    Thanks in advance!

    Also thanks for this forum, the information here has been very useful so far!

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Looking for a formula to extract a tax id from a string

    try this udf use as =extractstring(a1)
    it should pull any 2digit hypen 8digit string in this format xx-xxxxxxxx
    Function extractstring(s As String) As String
    Static rx As Object
    Dim rxMatches As Object
     
    If rx Is Nothing Then
        Set rx = CreateObject("VBScript.RegExp")
        With rx
            .Pattern = "\d{2}\-\d{8}"
            .Global = True
            .IgnoreCase = True
        End With
    End If
     
    With rx
        Set rxMatches = .Execute(s)
        If rxMatches.Count > 0 Then extractstring = rxMatches(rxMatches.Count - 1).Value
    End With
    End Function
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    10-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Looking for a formula to extract a tax id from a string

    After inputting =extractstring(a1) I get #NAME?.

    I am not sure if I input the function properly into visual basic. In the editor I pasted the function inside "ThisWorkbook" within VBA Project(excel file name)->Microsoft Excel Objects->ThisWorkbook. Is this correct?

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Looking for a formula to extract a tax id from a string

    paste it in a new module instead
    insert/module that should create and open module 1
    paste it in there
    this workbook has it in
    Attached Files Attached Files
    Last edited by martindwilson; 12-04-2013 at 03:20 PM.

  5. #5
    Registered User
    Join Date
    10-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Looking for a formula to extract a tax id from a string

    OK I see that it works in the attachment, so now the problem is how to integrate it without having to rebuild the rest of the spreadsheet around the new file. I have saved it in a new module 1 and then saved the entire file as a .xlsm file. On reopening I entered the function (it showed up on the auto fill suggestion drop down), but the result is a blank cell.

  6. #6
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Looking for a formula to extract a tax id from a string

    This User Defined Function (UDF) pulls only valid tax ID numbers (##-########)...ignoring patterns like 123-123456789 or 12-123456789...but allowing for values like: A12-12345678G

    To use it:
    • ALT+F11...to open the VBA Editor
    • Select your workbook from the VBAProject list
    • Insert.Module
    • Copy the below code and paste it into that module
    Function PullTaxID(ByVal CellText As String) As String
    
    Dim result As String
    Dim allMatches As Object
    Dim RE As Object
    CellText = " " & CellText & " "
    result = "no match"
    
    Set RE = CreateObject("vbscript.regexp")
    
    RE.Pattern = "\D\d{2,2}\-\d{8,8}\D"
    RE.Global = True
    RE.IgnoreCase = True
    Set allMatches = RE.Execute(CellText)
    
    If allMatches.Count <> 0 Then
        RE.Pattern = "\d{2,2}\-\d{8,8}"
        Set allMatches = RE.Execute(CellText)
        result = allMatches.Item(0)
    End If
    
    PullTaxID = result
    
    End Function
    To use that UDF to pull the tax ID from cell A1
    B1: =PullTaxID(A1)

    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  7. #7
    Registered User
    Join Date
    10-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Looking for a formula to extract a tax id from a string

    Ron,
    Unfortunately when using the pulltaxid formula with my data it results with "no match" quite often.

  8. #8
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Looking for a formula to extract a tax id from a string

    This code matches 2 numbers (not preceded by a number) followed by a dash followed by 7 numbers(not followed by a number):

    Function PullTaxID(ByVal CellText As String) As String
    
    Dim result As String
    Dim allMatches As Object
    Dim RE As Object
    CellText = " " & CellText & " "
    result = "no match"
    
    Set RE = CreateObject("vbscript.regexp")
    
    RE.Pattern = "\D\d{2,2}\-\d{7,7}\D"
    RE.Global = True
    RE.IgnoreCase = True
    Set allMatches = RE.Execute(CellText)
    
    If allMatches.Count <> 0 Then
        RE.Pattern = "\d{2,2}\-\d{7,7}"
        Set allMatches = RE.Execute(CellText)
        result = allMatches.Item(0)
    End If
    
    PullTaxID = result
    
    End Function

  9. #9
    Registered User
    Join Date
    10-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Looking for a formula to extract a tax id from a string

    Whoops it seems that my example data has 1 too many 1's, there are 2 digits before the - and then 7 digits following.

    Martin, I tried changing the line:
    .Pattern = "\d{2}\-\d{8}"
    to
    .Pattern = "\d{2}\-\d{7}"

    The results are still blank cells. Where else do I adjust? Thanks!
    Last edited by lychee; 12-04-2013 at 03:53 PM.

  10. #10
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Looking for a formula to extract a tax id from a string

    Can you post some samples that should match...but don't?

  11. #11
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Looking for a formula to extract a tax id from a string

    nothing that reg ex matches the pattern xx-xxxxxxx 2-7 so it should work post a sample like ron said
    and i have no idea what US tax numbers look like

  12. #12
    Registered User
    Join Date
    10-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Looking for a formula to extract a tax id from a string

    Ron-
    so close!!!! if the data says

    109912-3456789
    it says no match
    ditto 12-34567891099

    The data is so inconsistent

  13. #13
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Looking for a formula to extract a tax id from a string

    Ok...maybe this?
    Function PullTaxID(ByVal CellText As String) As String
    
    Dim result As String
    Dim allMatches As Object
    Dim RE As Object
    
    result = "no match"
    
    Set RE = CreateObject("vbscript.regexp")
    
    RE.Pattern = "\d{2}\-\d{7}"
    RE.Global = True
    RE.IgnoreCase = True
    Set allMatches = RE.Execute(CellText)
    
    If allMatches.Count <> 0 Then
        result = allMatches.Item(0)
    End If
    
    PullTaxID = result
    
    End Function

  14. #14
    Registered User
    Join Date
    10-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Looking for a formula to extract a tax id from a string

    Spectacular. I am in awe of you both. Cheers!

+ 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. Formula to extract numbers from string
    By Ninja2k in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-04-2013, 11:53 AM
  2. [SOLVED] what formula can I use to extract certain words from a string?
    By djmatok in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-03-2013, 05:21 PM
  3. what formula to extract text from a long string?
    By SEMMatt in forum Excel General
    Replies: 5
    Last Post: 10-07-2012, 10:23 PM
  4. formula to extract a section of of a text string
    By dcgrove in forum Excel General
    Replies: 6
    Last Post: 07-02-2010, 11:28 AM
  5. [SOLVED] Formula to extract digits from a text string?
    By Hash@example.org in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-15-2006, 12:16 AM

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