Deleting multiple rows based on different criteria VBA

simora33

New Member
Joined
Oct 9, 2017
Messages
2
Hi,

I am trying to create a macro that will delete multiple rows at once, based on different criteria. The rows need to be selected first and deleted simultaneously. My code seems to "forget" the first selection and only delete the rows from the last if-statement. Any help would be greatly appreciated. Here is what I have tried:

Code:
If condition1 = 0 Then
    Sheets("Sheet1").Select
    Range("1:1,2:2,4:4").Select
    Range("A4").Activate
        
    End If
    
If condition2 = 0 Then
    Sheets("Sheet1").Select
    Range("5:5,6:6,8:8").Select
    Range("A8").Activate
        
    End If
    
    Selection.Delete Shift:=xlUp
    
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Welcome to the board. It's not clear what the condition is, relative to row being deleted, i.e. why delete rows 1, 2 and 4 if condition1 = 0 and rows 5, 6, 8 if condition2 = 0.

The rows deleted seem to be of a similar pattern, e.g. x, x + 1, x + 3

Without benefit of seeing the worksheet you can see in front of you, can you provide more clarity (imagine you're explaining the problem to someone who is blind) or post the full macro you have?

I suspect you've tried to record a macro as you do not need to use .Select in manipulating objects so providing more information will help.
 
Upvote 0
Code:
Dim rng As Range
Sheets("Sheet1").Select
If condition1 = 0 Then Set rng = Range("1:2,4:4")
If condition2 = 0 Then
    If rng Is Nothing Then
        Set rng = Range("5:6,8:8")
    Else
        Set rng = Union(rng, Range("5:6,8:8"))
    End If
End If
rng.Delete
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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