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 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.