Forum Discussion
Ishmale
Jan 25, 2022Copper Contributor
Trying to use VBA to perform an Automated find and replace function
Hello, I am having trouble performing a find and replace with VBA. I am using a piece of code I modified for my work book. However I am running into a "compile error Expected list separator or )" . ...
Subodh_Tiwari_sktneer
Jan 25, 2022Silver Contributor
Please give this a try and let me know if this works as desired.
Remember to tweak the worksheets and ranges if not as per the sample data you provided.
Sub ReplaceOldUserCodeWithNewUserCode()
Dim wsData As Worksheet
Dim wsUser As Worksheet
Dim x As Variant
Dim dict As Object
Dim Rng As Range
Dim Cel As Range
Dim lr As Long
Dim i As Long
Application.ScreenUpdating = False
Set wsData = Worksheets("Daily Pick Data")
Set wsUser = Worksheets("Click USER Ref")
lr = wsData.Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = wsData.Range("A2:A" & lr)
x = wsUser.Range("A1").CurrentRegion.Value
Set dict = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(x, 1)
dict.Item(x(i, 1)) = x(i, 2)
Next i
For Each Cel In Rng
If dict.exists(Cel.Value) Then
Cel.Value = dict.Item(Cel.Value)
End If
Next Cel
Application.ScreenUpdating = True
End Sub
Ishmale
Jan 25, 2022Copper Contributor
Thank you, Subodh_Tiwari_sktneer.
I have found my error. When writing the lines:
Set myList = Sheets("Click USER Ref").Range("A2:B32")
Set myRange = Sheets("Daily Pick Data").Range("D5:D633")
Inside of the line I somehow mistakenly used "smart" quotes instead of "straight". Looking at the code it was a copy paste error. Upon re-writing the lines and not copying it the code works as intended.
I have found my error. When writing the lines:
Set myList = Sheets("Click USER Ref").Range("A2:B32")
Set myRange = Sheets("Daily Pick Data").Range("D5:D633")
Inside of the line I somehow mistakenly used "smart" quotes instead of "straight". Looking at the code it was a copy paste error. Upon re-writing the lines and not copying it the code works as intended.