VBA Bulk Find and Replace for Long Strings

lneidorf

Board Regular
Joined
May 20, 2004
Messages
97
Office Version
  1. 365
Platform
  1. Windows
Hi there.

I've got some code that I've been using for quite some time for bulk find and replace operations. I've got a tab with the values to find in col A and the values to replace with in col B. This macro looks through a range on another tab and performs find and replace operations en masse.

Here's the code:
Code:
Sub MultiFindNReplace()
    Dim myList, myRange

    Set myList = Sheets("Sheet1").Range("A1:B211") 'two column range where find/replace pairs are
    Set myRange = Sheets("Record List").Range("B2:B1550") 'range to be searched
    For Each cel In myList.Columns(1).Cells
        myRange.Replace what:=cel.Value, Replacement:=cel.Offset(0, 1).Value
    Next cel
End Sub

When I run this with my current sets of values, I'm getting Run-time 13 Type Mismatch errors.

I believe the issues is that the values I'm attempting to find / replace now exceed 255 characters. These values are long folder paths.

Any thoughts?

Thank you!
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You might consider .Substitute rather than .Replace...

Code:
Sub MultiFindNReplace2()
    Application.ScreenUpdating = False
    Dim myList As Range, myRange As Range, cel As Range, r As Range
    Set myList = Sheets("Sheet1").Range("A1:B211") 'two column range where find/replace pairs are
    Set myRange = Sheets("Record List").Range("B2:B" & Sheets("Record List").Cells(Rows.Count, "B").End(xlUp).Row) 'range to be searched
    For Each cel In myList.Columns(1).Cells
        For Each r In myRange
            r.Value = WorksheetFunction.Substitute(r.Value, cel.Value, cel.Offset(0, 1).Value)
        Next r
    Next cel
    Application.ScreenUpdating = True
End Sub

Cheers,

tonyyy
 
Upvote 0
You're welcome. Glad it worked out...
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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