VBA simplify code replace

I have a very complicated code that I think could be simplified.
For Column BB,
if "Fedex Letter" replace with  "Letter"
if "Fedex Pak" replace with  "Pak"
if anything else replace with "Box"

Can anyone help simplify?
Thanks!!

Sub ReplacePackages()


    Dim i As String
    Dim k As String
    Dim j As String
    Dim l As String
    Dim b As String
    Dim a As String
    Dim c As String
    Dim d As String
    Dim e As String
    Dim f As String
    Dim m As String
    Dim n As String
    Dim o As String
    Dim p As String
    Dim q As String
    Dim r As String
    
    a = "Customer Packaging"
    b = "Box"
    c = "Fedex Letter"
    d = "Letter"
    j = "Fedex Tube"
    l = "Box"
    e = "Fedex Pak"
    f = "Pak"
    i = "Fedex Box"
    k = "Box"
    r = "FedEx Large Box"
    m = "FedEx Small Box"
    n = "Fedex 25kg Box"
    o = "FedEx Medium Box"
    p = "Fedex 10kg Box"
    q = "Fedex 25kg Box"




    
    Columns("BB").Replace What:=i, replacement:=k, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=j, replacement:=l, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=e, replacement:=f, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=a, replacement:=b, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=c, replacement:=d, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=r, replacement:=b, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=m, replacement:=b, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=n, replacement:=b, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=o, replacement:=b, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=p, replacement:=b, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace What:=q, replacement:=b, LookAt:=xlPart, MatchCase:=False
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.

Ryan ChongCommented:
try:

Sub ReplacePackages()
    Dim i As String
    Dim k As String
    Dim j As String
    Dim l As String
    Dim b As String
    Dim a As String
    Dim c As String
    Dim d As String
    Dim e As String
    Dim f As String
    Dim m As String
    Dim n As String
    Dim o As String
    Dim p As String
    Dim q As String
    Dim r As String
    
    a = "Customer Packaging"
    b = "Box"
    c = "Fedex Letter"
    d = "Letter"
    j = "Fedex Tube"
    l = "Box"
    e = "Fedex Pak"
    f = "Pak"
    i = "Fedex Box"
    k = "Box"
    r = "FedEx Large Box"
    m = "FedEx Small Box"
    n = "Fedex 25kg Box"
    o = "FedEx Medium Box"
    p = "Fedex 10kg Box"
    q = "Fedex 25kg Box"
    
    Application.ScreenUpdating = False
    
    Columns("BB").Replace what:=c, Replacement:=d, LookAt:=xlPart, MatchCase:=False
    Columns("BB").Replace what:=e, Replacement:=f, LookAt:=xlPart, MatchCase:=False
    Range("BB2:BB20000").Select
    Range("BB2:BB20000").AutoFilter Field:=53, Criteria1:= _
        "<>*" & d & "*", Operator:=xlAnd, Criteria2:="<>*" & f & "*"
    Selection.FormulaR1C1 = l
    Range("BB1:BB20000").AutoFilter Field:=53
    
    Application.ScreenUpdating = True
End Sub

Open in new window

0
Roy CoxGroup Finance ManagerCommented:
maybe something like this

Option Explicit

Sub ReplacePackages()
'if "Fedex Letter" replace with  "Letter"
' if "Fedex Pak" replace with  "Pak"
' if anything else replace with "Box"
    Dim iX As Integer
    Dim sCrit As String, sReplace


    sCrit = Choose(iX, "Fedex Letter", "Fedex Pak")
    sReplace = Choose(iX, "Letter", "Pak")

    With ActiveSheet
        If Not .AutoFilterMode Then .Range("BB1").AutoFilter

        For iX = 1 To 3
            .Range("BB1").AutoFilter Field:=53, Criteria1:=sCrit
            On Error Resume Next
            .Range(.Cells(1, 53), .Cells(.Rows.Count, _
                                         53).End(xlUp)).SpecialCells(xlCellTypeVisible).Value = sReplace
            On Error GoTo 0
        Next iX

        .Range("BB1").AutoFilter Field:=1, Criteria1:="<>Letter", _
                                 Operator:=xlAnd, Criteria2:="<>Pak"
        .Range(.Cells(1, 53), .Cells(.Rows.Count, _
                                     53).End(xlUp)).SpecialCells(xlCellTypeVisible).Value = "Box"
        .Range("BB1").AutoFilter
    End With

End Sub

Open in new window

0
Saurabh Singh TeotiaCommented:
You can simply use this code which will do what you are looking for..

Sub Changevalues()
    Dim rng As Range, cell As Range
    Dim lrow As Long

    lrow = Cells(Cells.Rows.Count, "BB").End(xlUp).Row

    Set rng = Range("BB2:BB" & lrow)

    For Each cell In rng

        If InStr(1, cell.Value, "Fedex Letter", vbTextCompare) > 0 Then
            cell.Value = "Letter"
        ElseIf InStr(1, cell.Value, "Fedex Pak", vbTextCompare) > 0 Then
            cell.Value = "Pak"
        Else
            cell.Value = "Box"
        End If

    Next cell


End Sub

Open in new window


Saurabh...
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
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.