Not Deleting Selected Rows

OldRookie

New Member
Joined
Nov 12, 2020
Messages
36
Office Version
  1. 2013
Platform
  1. Windows
May I ask for your help with a row deletion problem that’s opposite of everything I’ve been able to find on my own? Instead of deleting selected rows in a specific range, I want to delete all the rows in a specific range that aren’t selected. I want to keep them. It’s sort of the negative/compliment/opposite to what I’ve been able to find. The use I envision is selecting the rows and pushing a form button that runs this code in a dedicated macro. Can this be done?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi OldRookie,

Try this while on the sheet with the data:

VBA Code:
Option Explicit
Sub Macro1()

    Dim rngKeep As Range, rngAllData As Range, rng As Range, rngDelete As Range
    
    Application.ScreenUpdating = False
    
    Set rngKeep = Selection
    Set rngAllData = ActiveSheet.UsedRange
    
    For Each rng In rngAllData.Rows
        If Intersect(rng, rngKeep.Rows) Is Nothing Then
            If rngDelete Is Nothing Then
                Set rngDelete = rng
            Else
                Set rngDelete = Union(rngDelete, rng)
            End If
        End If
    Next rng
    
    If Not rngDelete Is Nothing Then
        rngDelete.EntireRow.Delete
    End If
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution
Robert,
Wow! That works great! Even better, you used things I’ve never seen before. I’m looking forward to researching them out to understand and improve my skills.

Thank you so much!
 
Upvote 0
Robert,
Wow! That works great! Even better, you used things I’ve never seen before. I’m looking forward to researching them out to understand and improve my skills.

Thank you so much!

Thanks for letting us know and I'm glad we were able to provide you a suitable solution :cool:
 
Upvote 0

Forum statistics

Threads
1,215,254
Messages
6,123,893
Members
449,132
Latest member
Rosie14

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