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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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