Share via

Search and replace multiple values within string

Anonymous
2022-11-22T00:16:43+00:00

Looking for an automated solution (vba).

I have a dynamic list of old criteria (can be alphanumeric) and a list of new ones adjacent to it.

I need to automate a lookup/search and replace based on the list.

List has a named range if that helps make it faster.

Based on the lists in sheet "check", I need to search column L in sheet "audit" and replace any instance of the old numbers with the new numbers.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

OssieMac 48,001 Reputation points Volunteer Moderator
2022-11-22T23:52:24+00:00

A tip for the future: When including screen shots, it helps if both Column Id's and Row Id's are included. You can temporarily hide Columns and/or Rows to achieve this. I assumed that the data commences on row 3 in your screen shot so see the comment in my code where you might need to edit the start row.

Ensure that you have a backup of your workbook in case the code does not perform as required.

Sub FindAndReplace() 

    Dim wsAudit As Worksheet 

    Dim wsCheck As Worksheet 

    Dim rngReplace As Range 

    Dim rngOld As Range 

    Dim rngCel As Range 

    Dim rngToFind As Range 

    Set wsAudit = Worksheets("Audit") 

    Set wsCheck = Worksheets("Check") 

    With wsAudit 

        Set rngReplace = .Range(.Cells(2, "L"), .Cells(.Rows.Count, "L").End(xlUp)) 

    End With 

    With wsCheck 

        'Edit 3 in following line to your correct start row for Old list 

        Set rngOld = .Range(.Cells(3, "AA"), .Cells(.Rows.Count, "AA").End(xlUp)) 

    End With 

    For Each rngCel In rngOld 

        With rngReplace 

            Set rngToFind = .Find(What:=rngCel.Value, _ 

                                LookIn:=xlValues, _ 

                                LookAt:=xlWhole, _ 

                                SearchOrder:=xlByRows, _ 

                                SearchDirection:=xlNext, _ 

                                MatchCase:=False, _ 

                                SearchFormat:=False) 

            If Not rngToFind Is Nothing Then 

                Do 

                    rngToFind.Value = rngCel.Offset(0, 1).Value 

                    Set rngToFind = .FindNext(rngToFind) 

                    If rngToFind Is Nothing Then Exit Do 

                Loop 

            End If 

        End With 

    Next 

End Sub 

Feel free to get back to me if code does not perform as required.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. OssieMac 48,001 Reputation points Volunteer Moderator
    2022-11-25T20:35:40+00:00

    Works Great!

    When I make my first million.... I'm buying you a Krystal burger WITH cheese my friend!

    Or a White Castle burger if you prefer.

    With the number of these you have promised me, you might need to make more than a million. Anyway, I am happy to be able to assist you. Have a good day.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-11-25T18:33:28+00:00

    Works Great!

    When I make my first million.... I'm buying you a Krystal burger WITH cheese my friend!

    Or a White Castle burger if you prefer.

    Was this answer helpful?

    0 comments No comments