Find values in one list and replace them with values from another list

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
734
Office Version
  1. 365
Platform
  1. Windows
I have up to 1,000 entries (column A) on my Output worksheet. Some of them (100 or less) are in bad format or simply wrong. In a separate table on my Lookup worksheet, column A is populated with 'bad' entries that match the bad entries on the Output worksheet. Column B is populated with 'good' entries.

When working with smaller data sets and only one bad data point, I have used the following bit of code, which references the Output worksheet, column A. I know this is not good practice, since it can't be easily scaled.

Code:
On Error Resume Next
    For Each Rng In Range("A4:A" & La)
        If Rng.Value = "6070000000" Then
            Rng.Value = "'6071143E3"
        End If
    Next

I am seeking a VBA method to replace the bad entries on my Output worksheet based on columns A & B in my Lookup table.

How would I do this?

Thanks in advance for all replies.

Jim
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Re: Seeking method to find values in one list and replace them with values from another list

How about
Code:
Sub Mylookup()
   Dim cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("lookup")
      For Each cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         Dic(cl.Value) = cl.Offset(, 1).Value
      Next cl
   End With
   With Sheets("output")
      For Each cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.exists(cl.Value) Then cl.Value = Dic(cl.Value)
      Next cl
   End With
End Sub
 
Upvote 0
Re: Seeking method to find values in one list and replace them with values from another list

How about
Code:
Sub Mylookup()
   Dim cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("lookup")
      For Each cl In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         Dic(cl.Value) = cl.Offset(, 1).Value
      Next cl
   End With
   With Sheets("output")
      For Each cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.exists(cl.Value) Then cl.Value = Dic(cl.Value)
      Next cl
   End With
End Sub

Perfect!

Thanks for your prompt and excellent reply.

Jim
 
Upvote 0
Re: Seeking method to find values in one list and replace them with values from another list

Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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