goobee

New Member
Joined
Feb 25, 2011
Messages
26
I have a small macro that uses the State entries to add Sales Regions to a spreadsheet. The macro adds a new column right of the Zip Code column, copies over the States and then uses the find/replace function to enter the Sales Regions. It's most likely not the most efficient method but it more or less works for us. The issue we are having is when the State is blank, there is nothing to find and replace. Ideally, when State fields are blank, we would like to pull in the Zip Codes. I'm thinking this may be accomplished with a function to point the Sales Region cell to the corresponding Zip Code cell location. Is this possible? I've included snippets of the macro and data/desired output. (Fake data used) If not, alternate suggestions will be appreciated. Thanks for any assistance.

Code:
Sub Add_SalesRegions()

Application.ScreenUpdating = False

Rows(1).Find("State").EntireColumn.Select
Selection.Copy
Rows(1).Find("Zip Code").Offset(, 1).Select
ActiveSheet.Paste
Rows(1).Find("Zip Code").Offset(, 1).Select
ActiveCell = "SalesRegion"
Cells.EntireColumn.AutoFit
Rows(1).Find("SalesRegion").EntireColumn.Select

Selection.Replace what:="IA", Replacement:="Central", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Selection.Replace what:="IL", Replacement:="East", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Selection.Replace what:="MI", Replacement:="East", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Selection.Replace what:="MO", Replacement:="Central", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Selection.Replace what:="WI", Replacement:="Central", Lookat:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

Cells.EntireColumn.AutoFit

Application.ScreenUpdating = True

End Sub

CustomerNameAddressCityStateZip CodeSalesRegion
Getaway Inn234 E Cannon Ave.SaginawMI48605East
Home Furnishings Limited234 Heritage Ave.Midland4864048640
Johnson, Kimberly5678 S. 42nd Ave.RockfordIL61125East
Kensington Gardens Resort12345 Redmond RdMilwaukeeWI53204Central
Hampton Village Eatery234 Hampton VillageSpringfield6270362703
Healthy Concepts1234 Westown RoadWest Des MoinesIA50625Central
International Mailing Corp.8765 58th Street WestSt. LouisMO63156Central

<tbody>
</tbody>
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about
Code:
Sub goobee()
   Dim State As Range
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("IA", "Central", "IL", "East", "MI", "East", "MO", "Central", "WI", "Central")
   Set State = Range("1:1").Find("State", , , xlWhole, , , False, , False)
   With Cells(1, Columns.Count).End(xlToLeft)
      State.EntireColumn.Copy .Offset(, 1)
      .Offset(, 1).Value = "SalesRegion"
      For i = 0 To UBound(Ary) Step 2
         .Offset(, 1).Replace Ary(i), Ary(i + 1), xlWhole, , False, , False, False
      Next i
      With Range(.Offset(, 1), Cells(Rows.Count, .Column).End(xlUp).Offset(, 1))
         .SpecialCells(xlBlanks).FormulaR1C1 = "=rc[-1]"
         .Value = .EntireColumn.Value
      End With
   End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.

However I've just noticed that there's a slight problem, it should be
Code:
Sub goobee()
   Dim State As Range
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("IA", "Central", "IL", "East", "MI", "East", "MO", "Central", "WI", "Central")
   Set State = Range("1:1").Find("State", , , xlWhole, , , False, , False)
   With Cells(1, Columns.Count).End(xlToLeft)
      State.EntireColumn.Copy .Offset(, 1)
      .Offset(, 1).Value = "SalesRegion"
      For i = 0 To UBound(Ary) Step 2
         .Offset(, 1).Replace Ary(i), Ary(i + 1), xlWhole, , False, , False, False
      Next i
      With Range(.Offset(, 1), Cells(Rows.Count, .Column).End(xlUp).Offset(, 1))
         .SpecialCells(xlBlanks).FormulaR1C1 = "=rc[-1]"
         [COLOR=#ff0000].Value = .Value[/COLOR]
      End With
   End With
End Sub
 
Upvote 0
I think I broke it. For whatever reason, it is only pulling the States in now and not the Zones. That's my motto, "If it ain't broke, I'll keep working on it until it is."
 
Upvote 0
What code are you currently using?
 
Upvote 0

Forum statistics

Threads
1,215,263
Messages
6,123,954
Members
449,135
Latest member
jcschafer209

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