How to remove unmatched cell values using VBA

satish78

Board Regular
Joined
Aug 31, 2014
Messages
218
Hi Friends,

Looked on similar posts on this forum but did not find suitable solutions to my need.
Here what I am looking to get this done by vba macro.

I have list of data in Sheet1 and sheet2 with values to find in sheet1.
VBA macro to find matched sheet2(ColumnA) data in sheet1 anywhere and remove unmatched cell values from the range D:P
and move matched cell values in respective rows in Sheet1 columnD.

Sample file and Sample Results file attached.
Please take a look.

https://spaces.hightail.com/space/JjGtJaPocB
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Give this a try in a copy of your workbook.
If your data is very large this may be slow? If it is slow report back with about how many rows you actually have and confirm that D:P is the limit of columns to deal with.

Code:
Sub Remove_Unmatched()
  Dim a As Variant, itm As Variant
  
  With Sheets("Sheet2")
    a = .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Value
  End With
  With Sheets("Sheet1").Range("D:P")
    For Each itm In a
      .Replace What:=itm, Replacement:=Replace(itm, "@", "##"), LookAt:=xlWhole, MatchCase:=False
    Next itm
    .Replace What:="*@*", Replacement:=""
    .Replace What:="##", Replacement:="@", LookAt:=xlPart
    On Error Resume Next
    .SpecialCells(xlBlanks).Delete Shift:=xlToLeft
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
its fast on this small data.

Thanks Peter.

Give this a try in a copy of your workbook.
If your data is very large this may be slow? If it is slow report back with about how many rows you actually have and confirm that D:P is the limit of columns to deal with.

Code:
Sub Remove_Unmatched()
  Dim a As Variant, itm As Variant
  
  With Sheets("Sheet2")
    a = .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Value
  End With
  With Sheets("Sheet1").Range("D:P")
    For Each itm In a
      .Replace What:=itm, Replacement:=Replace(itm, "@", "##"), LookAt:=xlWhole, MatchCase:=False
    Next itm
    .Replace What:="*@*", Replacement:=""
    .Replace What:="##", Replacement:="@", LookAt:=xlPart
    On Error Resume Next
    .SpecialCells(xlBlanks).Delete Shift:=xlToLeft
    On Error GoTo 0
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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