if condition

anilg0001

Rules Violation
Joined
Jun 7, 2010
Messages
193
is else if work on VBA code
in my excel C range contain state abbrivation of usa
C1= AL
C2= CA
C150= AL

i want to change this as the full form
C1=Alabama
C2=California
C150=Alabama


i am trying to do like this
Dim M As Long

M = Sheets("Sheet3").Range("D" & Rows.Count).End(xlUp).Row
For M = M To 1 Step -1
With Range("D" & M)
If Range("D" & M) = " AL" Then Range("D" & M) = "Alabama"
Else: If Range("D" & M) = " AK" Then Range("D" & M) = "Alaska"
(CONTINUE)
End If
End With
Next M

is the correct?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hello,

No colon in the else if.

Like

If...then
elseif...then
elseif...
else
end if

Possibly try the select case:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> StateABV()<br><br><SPAN style="color:#00007F">Dim</SPAN> M <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br>M = Sheets("Sheet3").Range("D" & Rows.Count).End(xlUp).Row<br>    <br>    <SPAN style="color:#00007F">For</SPAN> M = M <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1<br>        <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> UCase(Range("D" & M))<br>            <SPAN style="color:#00007F">Case</SPAN> "AL"<br>                Range("D" & M).Value = "Alabama"<br>            Case "AK"<br>                Range("D" & M).Value = "Alaska"<br>            <SPAN style="color:#007F00">'Case "**"</SPAN><br>                <SPAN style="color:#007F00">'...</SPAN><br>            <SPAN style="color:#007F00">'Case "**"</SPAN><br>                <SPAN style="color:#007F00">'...</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> M<br><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
This should do it, I put a "space" ahead of each abbreviation because that's what you did.

Code:
Option Explicit

Sub ReplaceAbbreviations()
Dim Abbr As Variant
Dim States As Variant
Dim a As Long

Abbr = Array(" AL", " AK", " AR", " AZ", " CA", " CO", " CT", " DE", _
             " FL", " GA", " HI", " ID", " IL", " IN", " IA", " KS", _
             " KY", " LA", " ME", " MD", " MA", " MI", " MN", " MS", _
             " MO", " 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")
States = Array("Alabama", "Alaska", "Arkansas", "Arizona", "California", _
    "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", _
    "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", _
    "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", _
    "Missouri", "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")

For a = LBound(Abbr) To UBound(Abbr)
    ActiveSheet.Columns("D:D").Replace What:=Abbr(a), Replacement:=States(a), _
            LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
Next a


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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