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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,223,098
Messages
6,170,099
Members
452,301
Latest member
QualityAssurance

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