Replace multiple values to another value

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need to change a group of values to value A, and another group of values to value B
Below is what I have so far, and the code is not working. what am I doing wrong?
VBA Code:
Sub CleanUpOutlier()
    With Range("D1", Cells(Rows.Count, "D").End(3))
        .Replace "F21222CJ-08"",F21222CJ-11""", "F21222CJ"
        .Replace "F12315J-67"", F12314J-67""", "F12315J"
    End With
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
In this sample, there are only 4 items need to be changed. How about actual file?
Assume "F21222CJ-08" and "F21222CJ-11" need to be replaced with "F21222CJ"
and "F12315J-67" and "F12314J-67" need to be replaced with "F12315J"
try:
VBA Code:
Option Explicit
Sub CleanUpOutlier()
Dim lr&, i&, j&, rng, oldData, newData
lr = Cells(Rows.Count, "D").End(xlUp).Row
rng = Range("D1:D" & lr).Value
oldData = Array("F21222CJ-08", "F21222CJ-11", "F12315J-67", "F12314J-67") 'list of data need to be changed
newData = Array("F21222CJ", "F21222CJ", "F12315J", "F12315J") ' list of results, in corresponding order with oldData
For i = 1 To lr
    For j = 0 To UBound(oldData)
        If rng(i, 1) Like oldData(j) Then rng(i, 1) = newData(j)
    Next
Next
Range("D1:D" & lr).Value = rng
End Sub

In case there are lots of items need to be changed, try to create a lookup table.
 
Upvote 0

Forum statistics

Threads
1,215,278
Messages
6,124,021
Members
449,139
Latest member
sramesh1024

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