adding nonconnected cells to selection

RSXchin

Well-known Member
Joined
Oct 23, 2010
Messages
758
I'm looping through all rows on a worksheet, and I want to select several cells and then at the end, delete them all.

I CAN'T delete them as I come to them because they are dependent on each other. (Just for fun, I don't want to turn off calculations)

How do I add the rows to selection?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi,

Do you mean like this?

Code:
Range("A1,A3,A8,A16,A22,A26").Delete

Or is it more like this (delete cells that meet a criteria):

Code:
    lRow = Range("A" & Application.Rows.Count).End(xlUp).Row
    For i = lRow To 2 Step -1
        If Cells(i, 1).Value <> 0 Then Cells(i, 1).Delete
    Next
 
Upvote 0
Deleting cells/rows that meet certain criteria. But I can't delete them as I come to them, I have to delete them all at once.
 
Upvote 0
What difference does it make if you delete them all at once rather than one at a time?

You could maybe try this (not the best way, but it seems to work):

Code:
Sub test()
    Dim myRange As Range
    Dim lRow as Double
    Set myRange = Range("A1")
    lRow = Range("A" & Application.Rows.Count).End(xlUp).Row
    For i = lRow To 2 Step -1
        If Cells(i, 1).Value <> 0 Then
            Set myRange = Union(myRange, Range("A" & i))
        End If
    Next
    myRange.Delete
End Sub
 
Upvote 0
Sorry if I gave a weird example, I'm just trying to find out how to do a "union" of ranges. Until you told me, I had no idea what to call it, let alone implement it.
 
Upvote 0
Here we go. This way, MyRange can be empty to begin with
Code:
Sub union_test()
Dim MyRange As Range
Set MyRange = Nothing
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i, 1) = 2 Then
            Set MyRange = Cells(i, 1)
            GoTo 10
        End If
    Next i
    Exit Sub
10: For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i, 1) = 2 Then
            Set MyRange = Union(MyRange, Cells(i, 1))
        End If
    Next i
    MyRange.EntireRow.Delete shift:=xlUp
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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