VBA not deleting entire row at once

jimmy2

New Member
Joined
Feb 15, 2011
Messages
14
Hi Guys!

I have a feeling im missing something here but just having one of those days.

I have a 'List' that contains say 10000 values with duplicates and i have a 'template' containing unique values i need to filter by. My intention is to delete the entire row of values in 'List' that arent in the 'template'
I have managed to highlight the unmatched values by changing font colour (this works fine) but the deleting part isnt working. It seems to delete a few values at a time and i have to run the macro over 20 times!...Is there anything wrong with this?...Would welcome advice and recommendations?.


Code:
Application.ScreenUpdating = False
    For Each x In List
        count = 0
            For Each y In template
                If x.Value = y.Value Then
                    count = 1
                End If
            Next y
                If count = 0 Then
                    x.Font.ColorIndex = 4
                    x.EntireRow.Delete
                End If
    Next x
    
    Application.ScreenUpdating = True
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You normally have to work backwards when deleting rows. However, see if this works

Code:
Application.ScreenUpdating = False
For Each x In List
    For Each y In template
        If x.Value = y.Value Then
            If r Is Nothing Then
                Set r = x
            Else
                Set r = Union(r, x)
            End If
        End If
    Next y
Next x
If Not r Is Nothing Then r.EntireRow.Delete
Application.ScreenUpdating = True
 
Upvote 0
Re: VBA not deleting entire row at once*****RESOLVED**

You normally have to work backwards when deleting rows. However, see if this works

Code:
Application.ScreenUpdating = False
For Each x In List
    For Each y In template
        If x.Value = y.Value Then
            If r Is Nothing Then
                Set r = x
            Else
                Set r = Union(r, x)
            End If
        End If
    Next y
Next x
If Not r Is Nothing Then r.EntireRow.Delete
Application.ScreenUpdating = True
Code:
 Application.ScreenUpdating = False
    For Each x In List
        count = 0
            For Each y In template
                If x.Value = y.Value Then
                   count = 1
                End If
            Next y
            If count = 0 Then
                If r Is Nothing Then
                        Set r = x
                    Else
                        Set r = Union(r, x)
                End If
            End If
      Next x
      If Not r Is Nothing Then r.EntireRow.Delete
    
    Application.ScreenUpdating = True

Thanks!...changed it to the above and spot on!
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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