Search code examples
excelvbafind-replace

Cleaning VBA needed for Search and Replace


I am new in VBA so i just look for the code online and i modify it for my files.

I would like to know if i can put together the following 2 macros. It looks for contract number in column AF and then changes the Customer Name (col AN) & Customer Group Name (col AP). can it be donein another way?

I would like to simplify it as later i need, based on Contract Number to change 5 more variables in 5 different columns for other customers.

Sub CorrectCustomerNameXXXX()

Dim LastRow As Long
Dim i As Long

LastRow = Range("AF1000000").End(xlUp).Row
For i = LastRow To 1 Step -1
    If Range("AF" & i) = "006-0146157-001" Then
        Range("AN" & i).Value = "CUSTOMER_NAME"
    End If
Next
End Sub



Sub CorrectCustomerGROUPNameXXXX()

Dim LastRow As Long
Dim i As Long

LastRow = Range("AF1000000").End(xlUp).Row
For i = LastRow To 1 Step -1
    If Range("AF" & i) = "006-0146157-001" Then
        Range("AP" & i).Value = "CUSTOMER_GROUP"
    End If
Next

End Sub

Thanks in advance for your help


Solution

  • Try this code, please. You should use the same iteration:

    Sub CorrectWhatever()
    Dim LastRow As Long, i As Long
    
    LastRow = Range("AF" & rows.count).End(xlUp).Row
    For i = 1 To LastRow
        If Range("AF" & i).Value = "006-0146157-001" Then
            Range("AN" & i).Value = "CUSTOMER_NAME"
            Range("AP" & i).Value = "CUSTOMER_GROUP"
        End If
    Next
    End Sub