VBA replace based on values in column J

The following code should replace the text based on the text in column J.
If it is "US", use one replace, if not, use another.
However, when i run it, it replaces all with the second condition - even in "US" is in column J, it uses the second set.
Can anyone help?


Sub Replace_Zones()

    If Cells(2, "J") = "US" Then
    
        Columns("C").Replace What:="A", Replacement:="2", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="B", Replacement:="3", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="C", Replacement:="4", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
            Columns("C").Replace What:="D", Replacement:="5", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="E", Replacement:="6", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
        Columns("C").Replace What:="F", Replacement:="7", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="G", Replacement:="8", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="H", Replacement:="9", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="I", Replacement:="10", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="J", Replacement:="11", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
          Columns("C").Replace What:="K", Replacement:="12", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="L", Replacement:="13", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="M", Replacement:="14", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="N", Replacement:="15", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
          Columns("C").Replace What:="O", Replacement:="16", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
    Else
    
        Columns("C").Replace What:="A", Replacement:="2", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="C", Replacement:="3", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="D", Replacement:="4", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
            Columns("C").Replace What:="E", Replacement:="5", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="F", Replacement:="6", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
        Columns("C").Replace What:="G", Replacement:="7", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="H", Replacement:="8", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="I", Replacement:="9", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="J", Replacement:="10", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="K", Replacement:="11", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
          Columns("C").Replace What:="L", Replacement:="12", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="M", Replacement:="13", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="N", Replacement:="14", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
         Columns("C").Replace What:="O", Replacement:="15", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
          Columns("C").Replace What:="P", Replacement:="16", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End If

End Sub

Open in new window

Euro5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
Can you post a sample workbook to test? There is nothing obvious as to why it wouldn't work. It very clearly checks if cell J2 contains "US".
0
Euro5Author Commented:
Wayne,
In the attached, the US should show 5 and the CA should show 4, both are D.
But they all show 5 when I run.
Any ideas?
Thanks!
test-for-zones.xlsm
0
Wayne Taylor (webtubbs)Commented:
OK, your code only ever looks at cell J2, not all rows in column J. You will need to loop through each cell and do the replace one by one.

Sub Replace_Zones2()

    Dim cell As Range
    For Each cell In Range([J2], Cells(Rows.Count, "J").End(xlUp))
        If cell = "US" Then
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "A", "2")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "B", "3")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "C", "4")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "D", "5")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "E", "6")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "F", "7")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "G", "8")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "H", "9")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "I", "10")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "J", "11")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "K", "12")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "L", "13")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "M", "14")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "N", "15")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "O", "16")
        Else
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "A", "2")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "C", "3")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "D", "4")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "E", "5")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "F", "6")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "G", "7")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "H", "8")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "I", "9")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "J", "10")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "K", "11")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "L", "12")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "M", "13")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "N", "14")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "O", "15")
            Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "P", "16")
        End If
    Next
    
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Euro5Author Commented:
I understand  - thanks so much! :)
0
Euro5Author Commented:
Wayne,
I am getting wrong number of arguments or invalid property assignment error.          
At Replace in line 6.
 Cells(cell.Row, "C") = Replace(Cells(cell.Row, "C"), "A", "2")
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.