SamNew2Coding
New Member
- Joined
- Jun 15, 2020
- Messages
- 11
- Office Version
- 365
- Platform
- Windows
I have a list of correct and incorrect names as seen in sample file below. I am trying to loop through a list of sheets and do a find and replace for each item on the List. The Correct Name is what the replace is going to be and the Incorrect Name is the find. I have looped through specific sheets using arrays before but i cant figure out how to incorporate cell values from a variable sized list in the middle of the loop.
In the end, i just want to do a find and replace for a list of find and replacement data and perform that replacement in just select sheets. The issue is the amount of find and replaces will vary depending om how long the replacement list is.
I have started with code i found someone else upload to a forum and started adding a value lookup for the first row of data but stuck here.
In the end, i just want to do a find and replace for a list of find and replacement data and perform that replacement in just select sheets. The issue is the amount of find and replaces will vary depending om how long the replacement list is.
Book1 | ||||
---|---|---|---|---|
A | B | |||
1 | Incorrect Name | Correct Name | ||
2 | Item100 | Pencil | ||
3 | Item101 | Pen | ||
4 | Item102 | Paper | ||
5 | Item103 | Staple | ||
6 | Item104 | Marker | ||
Sheet1 |
I have started with code i found someone else upload to a forum and started adding a value lookup for the first row of data but stuck here.
VBA Code:
Dim ws As Worksheet
Dim stFind As String, stReplace As String
stFind = Sheets("Data").Range("A2").Value
stReplace = Sheets("Data").Range("B2").Value
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case "Sheet1", "Sheet2", "Sheet3", "Sheet4"
ws.Cells.Replace What:=stFind, Replacement:=stReplace, LookAt:=xlWhole, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Case Else
End Select
Next ws