Looking to select noncontiguous rows with For loop
I don't think you gain anything trying to select all the rows first (Union's
can become slow as the numbers increase); just delete them as you go (but
run the loop backwards). Give this a try (on a copy of your worksheet to be
safe)...
Sub RemoveRows()
Dim ZeroRow As Long, X As Long
Const Srow As Long = 13 ' Start row
Const Incr As Long = 8 ' Increment amount
With ActiveSheet
ZeroRow = 1000
For X = Srow + Incr * ((ZeroRow - Srow - 6) \ Incr) To Srow Step -8
.Rows(X).Delete
Next
End With
End Sub
--
Rick (MVP - Excel)
"Bishop" wrote in message
...
I suppose the way I worded it is a little confusing. What I mean when I
say
"ALL" rows is every 8th row starting with row 13 and ending with row
(ZeroRow
- 7). I just assumed that seeing "noncontiguous" in the subject line and
seeing "Step 8" in my code one would "assume"... Of course, we all know
what
happens when one assumes :) What meant was I wanted to select every 8th
row
between 13 and (ZeroRow - 7) and delete them ALL at once. Sorry for the
confusion.
"Rick Rothstein" wrote:
Your question... the Step 8 part coupled with your statement "select ALL
rows from 13 to (ZeroRow - 7)"... is kind of confusing. However, I would
point out that you do not have to physically select a range in order to
delete it. This statement will delete ALL rows between (and including) 13
through ZeroRow-7...
Range("A13:A" & (ZeroRow - 7)).EntireRow.Delete
Note: Since we are deleting the entire row, it doesn't matter which
column
we choose a cell from on that row in order to extend it through to the
entire row, so I used Column A.
--
Rick (MVP - Excel)
"Bishop" wrote in message
...
I have the following code:
For i = 13 To (ZeroRow - 7) Step 8
.Rows(i).Select
Next
.Selection.Delete
What I'm trying to do is select ALL rows from 13 to (ZeroRow - 7) THEN
delete them all at once. With the code written the way it is only the
very
last row selected will be deleted. How do I get a "hold the control
button
and select multiple rows" kind of selection using VBA code?
|