Chaz6
New Member
- Joined
- Apr 3, 2012
- Messages
- 3
I wish to compare two worksheets and remove all the rows from worksheet 1 where there is no corresponding row in worksheet 2. I wish to compare column C in worksheet 2 starting at row 8 with column B in worksheet 2 starting at row 2. I have almost got it working, except that I have to run the procedure thrice otherwise I still have rows left that should have been removed in worksheet 1. What do I need to do to get rid of the "For i = 1 to 3" loop?
Code:
Set Dict1 = CreateObject("Scripting.Dictionary")
Dim Range2 As Range
Sheets("import").Select
For Each Range2 In Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
If Not Dict1.Exists(Range2.Value2) Then
Dict1.Add Range2.Value2, ""
End If
Next Range2
Set Range2 = Nothing
Sheets("data").Select
For i = 1 To 3
For Each Range2 In Range(Range("C8"), Range("C" & Rows.Count).End(xlUp))
If Not Dict1.Exists(Range2.Value2) Then
Rows(Range2.Row).Delete shift:=xlUp
End If
Next Range2
Next i
Set Dict1 = Nothing
Set Range2 = Nothing