Removing rows with VBA

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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
When deleting rows in Excel you should always work up the sheet from the bottom up or you end up skipping rows that should be deleted, hence why you find you need to run the code three times.

Dom
 
Upvote 0
Going on from Domski's reply, if you create a column of numbers and then write a loop in the style that you have to delete odd numbers, you'll see that it "skips" rows.

One way around this, if you don't want to delete from the bottom up would be to filter for values you want deleted and then delete visible rows (excluding the header) only. Depending on the size of your data set, this may be a faster approach
 
Upvote 0
Thank you both. I shall try the approaches you suggested. I am disappointed it isn't as simple as I thought!
 
Upvote 0
This is all the change you would need:

Code:
    Set Dict1 = CreateObject("Scripting.Dictionary")
    Dim Range2 As Range
    Dim lngLoopRow As Long

    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 lngLoopRow = Range("C" & Rows.Count).End(xlUp).Row To 8 Step -1
        If Not Dict1.Exists(Range("C" & lngLoopRow).Value2) Then
            Rows(lngLoopRow).Delete shift:=xlUp
        End If
    Next lngLoopRow

    Set Dict1 = Nothing
    Set Range2 = Nothing

Dom
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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