Convert State Names to State Initials

miketran

New Member
Joined
Jul 15, 2011
Messages
14
Hello,

I found the solution below in this forum and it is very close to what I am looking for, but not quite. I have a list of contacts that has a mix of state names and states initials in the STATE column...The script below just creates a new column...I need a VBA script that will go down the list and replace the NAME of the state with the state abbreviation. Some of the state cells have bad data in them, so those cells need to be skipped...


Sub GetStateNames()
Const StateNames As String = _ "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida," & _ "Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine," & _ "Maryland,Massachusetts,Michigan,Mississippi,Missouri,Minnesota,Montana,Nebraska," & _ "Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota," & _ "Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee," & _ "Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming"

Const StateIds As String = _ "AL,AK,AZ,AR,CA,CO,CT,DE,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MS,MO,MN,MT," & _ "NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY"

Dim vecStateNames As Variant
Dim vecStateIds As Variant
Dim cell As Range

vecStateIds = Split(StateIds, ",")
vecStateNames = Split(StateNames, ",")

For Each cell In Range("A2:A200")

If cell.Value <> "" Then
cell.Offset(0, 1).Value = Application.Index(vecStateNames, Application.Match(cell.Value, vecStateIds, 0))

End If
Next cell
End Sub





From this:
AL
bad data
Alaska
CA
New Jersey

To This:

AL
bad data
AK
CA
NJ

Any Ideas?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this modification.
Code:
Sub GetStateNames()
Const StateNames As String = _
"Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,Florida," & _
"Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine," & _
"Maryland,Massachusetts,Michigan,Mississippi,Missouri,Minnesota,Montana,Nebraska," & _
"Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota," & _
"Ohio,Oklahoma,Oregon,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee," & _
"Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming"

Const StateIds As String = _
"AL,AK,AZ,AR,CA,CO,CT,DE,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,MA,MI,MS,MO,MN,MT," & _
"NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY"

Dim vecStateNames As Variant
Dim vecStateIds As Variant
Dim cell As Range

vecStateIds = Split(StateIds, ",")
vecStateNames = Split(StateNames, ",")

For i = LBound(vecStateNames) To UBound(vecStateNames)
    'Set the range to suit
    Range("A2:A200").Replace vecStateNames(i), vecStateIds(i)
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

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