Extracting State Abbreviation from a String

ESACAWIP

New Member
Joined
Nov 9, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hello! Having some trouble finding out a way to extract any 2 letter state abbreviations from a string. Most of what I've googled returns pull state values from a address, but in my data, its more like random sentences and words.
-if multiple states found, they will need to be separated by a comma
-the state will always be capitalized in the string (eg. FL, TX, MO)
-in some tricky instances like "(CALIFORNIA and TEXAS and CO)", only the state value "CO" will be pulled since California and Texas are not in the abbreviated form

Much apprevaited!

today is the day in CA where the sun setsCA
DE is quite far from FLDE, FL
NYNY
(CALIFORNIA and TEXAS and CO)CO
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This should do it.

EXCEL
ABC
1today is the day in CA where the sun setsCACA
2DE is quite far from FLDE, FLDE, FL
3NYNYNY
4(CALIFORNIA and TEXAS and CO)COCO
Sheet1
Cell Formulas
RangeFormula
C1:C4C1=STATE(B1)


VBA Code:
Function STATE(s As String)
Dim AL As Object:   Set AL = CreateObject("System.Collections.ArrayList")
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "[A-Z]{2}"
    Set matches = .Execute(s)
    For Each m In matches
        AL.Add m.Value
    Next m
End With
STATE = Join(AL.toarray, ", ")
End Function

Thank you for the fast response lrobbo314

Its working for all cells except C4 "(CALIFORNIA and TEXAS and CO)" im getting a strange output. Its returning "CA, LI, FO, RN, IS, TE, XA, CO"
 
Upvote 0
Try this UDF:
VBA Code:
Function to_State(a As String) As String
Dim tx As String
Dim Matches As Object, m
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "\b[A-Z]{2}\b"
        If .test(a) Then
            Set Matches = .Execute(a)
            For Each m In Matches
              tx = tx & ", " & m
            Next
        End If
End With
to_State = Mid(tx, 3)
End Function
 
Upvote 0
Hi, here's another option that seems to work for the examples presented.

Book1
AB
1today is the day in CA where the sun setsCA
2DE is quite far from FLDE, FL
3NYNY
4(CALIFORNIA and TEXAS and CO)CO
Sheet1
Cell Formulas
RangeFormula
B1:B4B1=LET(TS,TEXTSPLIT(A1,{" ","(",")"}),TEXTJOIN(", ",1,FILTER(TS,(LEN(TS)=2)*EXACT(TS,UPPER(TS)))))
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top