Comparing columns across workbooks !

svkroy

Board Regular
Joined
Sep 12, 2009
Messages
179
Hi,

I have two workbooks. 1.xlsx & 2.xlsx.

I would like to match the cell values of column A (has headers) of 1.xlsx with that of Coulumn A (has headers) of 2.xlsx (there would be duplicates in both).

If they don't match then i would like to delete the row pertaining to that cell in 1.xlsx.

Till now, after some clues, I figured out this (but unfortunately its deleting everything).

Sub no2()

Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim Rng As Range
Dim wbMaster As Workbook
Dim wsMaster As Worksheet
Dim Rng2 As Range
Dim Cell As Range
Dim foundcell As Range

Workbooks.Open ("D:\1.xlsx")
Workbooks.Open ("D:\2.xlsx")

Set wbSource = Workbooks("2.xlsx")
Set wsSource = wbSource.Worksheets("Sheet1")
With wsSource
Set Rng = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With

Set wbMaster = Workbooks("1.xlsx")
Set wsMaster = wbMaster.Worksheets("Sheet1")
With wsMaster
Set Rng2 = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With

For Each Cell In Rng

Set foundcell = Rng2.Find(what:=Cell.Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False)

If Not foundcell Is Nothing Then
Rownum = foundcell.Row
Else
Rng.EntireRow.Delete
End If


Next Cell

End Sub



Any help is much appreciated.
 
I don't know if it matters but deleting rows in a range can sometimes cause issues when you are simultaneously looping the rows (being that parts of the range are disappearing as you go). You could try instead, if Rng is only one column:

Code:
Dim i As Long

For i = Rng.Cells.Count to 1 Step -1 '//Assumes Rng is One Column Only

	Set foundcell = Rng2.Find(what:=Rng.Cells(i).Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False)

	If Not foundcell Is Nothing Then
		Rng.Cells(i).EntireRow.Delete
	End If
	
Next i

The difference here is we move bottom to top, so as we take off rows there's no effect on the rows above which is the direction in which we are going through the loop. For what it's worth, I would typically just add a column, run down a Match() formula, and delete the matched rows -- filtering makes this go pretty quick.

ξ
 
Last edited:
Upvote 0
Thanks a lot ! Worked flawlessly.........Will try the filter option if this takes too much time. Kudos !:cool:


I don't know if it matters but deleting rows in a range can sometimes cause issues when you are simultaneously looping the rows (being that parts of the range are disappearing as you go). You could try instead, if Rng is only one column:

Code:
Dim i As Long

For i = Rng.Cells.Count to 1 Step -1 '//Assumes Rng is One Column Only

	Set foundcell = Rng2.Find(what:=Rng.Cells(i).Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False)

	If Not foundcell Is Nothing Then
		Rng.Cells(i).EntireRow.Delete
	End If
	
Next i

The difference here is we move bottom to top, so as we take off rows there's no effect on the rows above which is the direction in which we are going through the loop. For what it's worth, I would typically just add a column, run down a Match() formula, and delete the matched rows -- filtering makes this go pretty quick.

ξ
 
Upvote 0

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