Trying to use VBA to perform an Automated find and replace function

%3CLINGO-SUB%20id%3D%22lingo-sub-3071344%22%20slang%3D%22en-US%22%3ETrying%20to%20use%20VBA%20to%20perform%20an%20Automated%20find%20and%20replace%20function%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3071344%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3EI%20am%20having%20trouble%20performing%20a%20find%20and%20replace%20with%20VBA.%20I%20am%20using%20a%20piece%20of%20code%20I%20modified%20for%20my%20work%20book.%20However%20I%20am%20running%20into%20a%20%22compile%20error%20Expected%20list%20separator%20or%20)%22%20.%20I%20do%20not%20see%20the%20issue%20at%20this%20time%20and%20I%20have%20been%20trying%20to%20research%20the%20root%20causes%20behind%20the%20message%20but%20I%20do%20not%20see%20in%20the%20code%20why%20it%20would%20cause%20it%20at%20this%20time.%20I%20am%20using%20Office%20365.%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20purpose%20of%20my%20code%20is%20to%20look%20at%20Daily%20Pick%20Data%20worksheet%20with%20a%20column%20that%20has%20some%20old%20user%20codes%20in%20it%20and%20replace%20them%20using%20a%20table%20on%20a%20Click%20USER%20Ref%20worksheet%20that%20has%20the%20new%20user%20code.%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20automate%20this%20process%20because%20I%20will%20be%20pulling%20this%20data%20daily%20and%20do%20not%20want%20to%20find%20and%20replace%20manually%20or%20create%20extra%20columns%20just%20to%20use%20the%20lookup%20fx(s).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20is%20the%20following%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20multiFindandReplace()%3C%2FP%3E%3CP%3EDim%20myList%3CBR%20%2F%3EDim%20myRange%3C%2FP%3E%3CP%3ESet%20myList%20%3D%20Sheets(%E2%80%9CClick%20USER%20Ref%E2%80%9D).Range(%E2%80%9CA2%3AB24%E2%80%9D)%3CBR%20%2F%3ESet%20myRange%20%3D%20Sheets(%E2%80%9CDaily%20Pick%20Data%E2%80%9D).Range(%E2%80%9CD5%3AD515%E2%80%9D)%3C%2FP%3E%3CP%3EFor%20Each%20cel%20In%20myList.Columns(1).Cells%3C%2FP%3E%3CP%3EmyRange.Replace%20What%3A%3Dcel.Value%2C%20Replacement%3A%3Dcel.Offset(0%2C%201).Value%2C%20LookAt%3A%3DxlWhole%3C%2FP%3E%3CP%3ENext%20cel%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3071344%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

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 )" . I do not see the issue at this time and I have been trying to research the root causes behind the message but I do not see in the code why it would cause it at this time. I am using Office 365. 

The purpose of my code is to look at Daily Pick Data worksheet with a column that has some old user codes in it and replace them using a table on a Click USER Ref worksheet that has the new user code.

I am trying to automate this process because I will be pulling this data daily and do not want to find and replace manually or create extra columns just to use the lookup fx(s).

 

The code is the following:

 

Sub multiFindandReplace()

Dim myList
Dim myRange

Set myList = Sheets(“Click USER Ref”).Range(“A2:B24”)
Set myRange = Sheets(“Daily Pick Data”).Range(“D5:D515”)

For Each cel In myList.Columns(1).Cells

myRange.Replace What:=cel.Value, Replacement:=cel.Offset(0, 1).Value, LookAt:=xlWhole

Next cel

End Sub

 

Thank you.

 

6 Replies

@Ishmale 

How about uploading a sample file with some dummy data in both the worksheets along with the logic you are trying to replace data. That would help to suggest a working solution considering your existing data.

@Subodh_Tiwari_sktneer 

For some reason I can Not upload a file or at least do not see an option to upload a file.

 

I would like to search the user code column in the "Daily Pick Data" sheet and then using the list in the "Click USER Ref" sheet find the old code and replace the "Daily Pick Data" cell with the new user code from the table on "Click USER Ref". 

 

Sorry for not being able to post the upload the file.

Ishmale_0-1643115238503.png

Ishmale_2-1643115307606.png

 

Ishmale_3-1643115349424.png

 

 

The range is no longer valid because I am now using the sample doc and not original.

@Ishmale 

 

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
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.