I am looking to make an excel script that can find and replace data, but for the love of everything I cannot figure out how to write it.
So the macro would look at the data in a cell in column B, then look at the adjacent cell in column C, and replace all instances of that data in column A with what if found in C. So the results would be:
But that’s not all, I would like it to not change cells in A that already have been changed once! (I’m trying this with changing the background colour)
Any help? I am at a complete loss.
Okay I found out how to do the easy part (replacing), but I cannot find out how to not change cells that already have been changed once. Here is my code:
Sub multiFindNReplace() Dim myList, myRange Set myList = Sheets("sheet1").Range("A2:B3") 'two column range where find/replace pairs are Set myRange = Sheets("sheet1").Range("D2:D5") 'range to be searched For Each cel In myList.Columns(1).Cells myRange.Replace what:=cel.Value, replacement:=cel.Offset(0, 1).Value, ReplaceFormat:=True Next cel End Sub
As far as I can tell, ReplaceFormat:=True
doesn’t do anything ;/ so items that already have been replaced once still are being replaced! Is there a way to somehow make this work?
Here’s the answer using your recommendation with color as a one-time limiter:
Sub Replace_Once() 'Find last row using last cell in Column B LastRow = Range("B" & Rows.Count).End(xlUp).Row 'Clear colors in Column A Range("A1:A" & LastRow).Interior.ColorIndex = xlNone 'Look at each cell in Column B one at a time (Cel is a variable) For Each Cel In Range("B1:B" & LastRow) 'Compare the cell in Column B with the Value in Column A one at a time (C is a variable) For Each C In Range("A1:A" & LastRow) 'Check if the Cell in Column A matches the Cell in Column B and sees if the color has changed. If C.Value = Cel.Value And C.Interior.Color <> RGB(200, 200, 200) Then 'Colors the cell C.Interior.Color = RGB(200, 200, 200) 'Updates the value in Column A with the cell to the right of the Cell in Column B C.Value = Cel.Offset(0, 1).Value End If Next Next 'Uncomment the line below to remove color again 'Range("A1:A" & LastRow).Interior.ColorIndex = xlNone End Sub