Find and replace multiple values from a list

ryanpetersen

New Member
Joined
Sep 1, 2014
Messages
5
Hi,


I was wondering if someone could please help me with the following.


I have 'Table' worksheet with sales figures and I'm trying to work out how to make a macro go through a list on the 'Lookup' worksheet, find and replace the values in the 'Table' worksheet.
For example, to search "Product 2" in column A of 'Table' worksheet and replace with "DELETED Product 2" (data from the 'Lookup' worksheet). This would continue until all values have been checked and replaced. Also on the 'Table' worksheet, a product might appear multiple times. For example Product 2 might need to be replaced 3 or 4 times before it starts replacing "Product 4" with "DELETED Product 4"
The end result I'm trying to get to is similar to 'Outcome' below.


Many thanks for your help.

Table
Product 1 $100,000
Product 2 $5,000
Product 3 $120,000
Product 4 $2,000

Lookup
Product 2 = DELETED Product 2
Product 4 = DELETED Product 4

Outcome
Product 1 $100,000
DELETED Product 2 $5,000
Product 3 $120,000
DELETED Product 4 $2,000
 
Code:
Sub Macro1()
    lastLine = Sheets("Table").Range("A" & Rows.Count).End(xlUp).Row
    For Each Cell In Sheets("Outcome").Range("A1:A" & lastLine).Cells
        i = i + 1
        Sheets("Outcome").Range("A" & i).Value = "=IF(IFERROR(VLOOKUP(Table!A" & i & ",Lookup!A:B,2,FALSE),Table!A" & i & ")=0,"""",IFERROR(VLOOKUP(Table!A" & i & ",Lookup!A:B,2,FALSE),Table!A" & i & "))"
        Sheets("Outcome").Range("A" & i).Value = Sheets("Outcome").Range("A" & i).Value
        Sheets("Outcome").Range("B" & i).Value = "=Table!B" & i
        Sheets("Outcome").Range("B" & i).Value = Sheets("Outcome").Range("B" & i).Value
    Next Cell
End Sub
 
Last edited:
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Actually the code works but doesn't make sense. Use this instead.

Code:
Sub Macro1()     lastLine = Sheets("Table").Range("A" & Rows.Count).End(xlUp).Row
    i = 0     Do Until i = lastLine + 1         i = i + 1         Sheets("Outcome").Range("A" & i).Value = "=IF(IFERROR(VLOOKUP(Table!A" & i & ",Lookup!A:B,2,FALSE),Table!A" & i & ")=0,"""",IFERROR(VLOOKUP(Table!A" & i & ",Lookup!A:B,2,FALSE),Table!A" & i & "))"         Sheets("Outcome").Range("A" & i).Value = Sheets("Outcome").Range("A" & i).Value         Sheets("Outcome").Range("B" & i).Value = "=Table!B" & i         Sheets("Outcome").Range("B" & i).Value = Sheets("Outcome").Range("B" & i).Value     Loop End Sub</pre>
 
Last edited:
Upvote 0
Code:
Sub M_snb()
   sn=sheets("lookup").cells(1).currentregion

   for j=1 to ubound(sn)
      sheets("table").columns(1).replace sn(j,1),"deleted"
   next
End Sub
 
Upvote 0
Actually the code works but doesn't make sense. Use this instead.

Code:
Sub Macro1()     lastLine = Sheets("Table").Range("A" & Rows.Count).End(xlUp).Row
    i = 0     Do Until i = lastLine + 1         i = i + 1         Sheets("Outcome").Range("A" & i).Value = "=IF(IFERROR(VLOOKUP(Table!A" & i & ",Lookup!A:B,2,FALSE),Table!A" & i & ")=0,"""",IFERROR(VLOOKUP(Table!A" & i & ",Lookup!A:B,2,FALSE),Table!A" & i & "))"         Sheets("Outcome").Range("A" & i).Value = Sheets("Outcome").Range("A" & i).Value         Sheets("Outcome").Range("B" & i).Value = "=Table!B" & i         Sheets("Outcome").Range("B" & i).Value = Sheets("Outcome").Range("B" & i).Value     Loop End Sub

It got all messed up when I copied it. Here it the right one.
Actually the code works but doesn't make sense. Use this instead.

Code:
Sub Macro1()     
     lastLine = Sheets("Table").Range("A" & Rows.Count).End(xlUp).Row
     i = 0     
     Do Until i = lastLine + 1         
          i = i + 1         
           Sheets("Outcome").Range("A" & i).Value =  "=IF(IFERROR(VLOOKUP(Table!A" & i &  ",Lookup!A:B,2,FALSE),Table!A" & i &  ")=0,"""",IFERROR(VLOOKUP(Table!A" & i &  ",Lookup!A:B,2,FALSE),Table!A" & i & "))"         
          Sheets("Outcome").Range("A" & i).Value = Sheets("Outcome").Range("A" & i).Value     
          Sheets("Outcome").Range("B" & i).Value = "=Table!B" & i         
          Sheets("Outcome").Range("B" & i).Value = Sheets("Outcome").Range("B" & i).Value     
     Loop 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

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