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?
Bookmarks