Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Looking to select noncontiguous rows with For loop

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
r r is offline
external usenet poster
 
Posts: 125
Default Looking to select noncontiguous rows with For loop

Sub Test_row_select()

Dim ZeroRow As Long, i As Long
Dim Rng As Excel.Range
With ActiveSheet
ZeroRow = 1000
For i = 13 To (ZeroRow - 7) Step 8
If TypeName(Rng) = "Nothing" Then
Set Rng = .Cells(i, 1).EntireRow
Debug.Print Rng.Address
Else
Set Rng = Application.Union(Rng, .Cells(i, 1).EntireRow)
End If
Next
Rng.Select
End With
End Sub

regards
r

Il mio ultimo lavoro ...
http://excelvba.altervista.org/blog/...ternative.html


"Bishop" wrote:

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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Looking to select noncontiguous rows with For loop

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?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Looking to select noncontiguous rows with For loop

rows(i).Delete

works too

"Rick Rothstein" wrote in message
...
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?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Looking to select noncontiguous rows with For loop

Well, to do it all at once like the code line I posted (again, ignoring the
Step 8 increment which I still don't see how it applies), you would need
something like this...

Rows(13).Resize(ZeroRow - 19).Delete

where the 19 would be a combination of the 7 that the OP wanted to subtract
from the ZeroRow value plus one to make the subtraction include the row at
both ends of the interval). I was going to post this solution, but figured
the Range method I presented would be easier to understand and remember in
future situations where it may be needed.

--
Rick (MVP - Excel)


"Patrick Molloy" wrote in message
...
rows(i).Delete

works too

"Rick Rothstein" wrote in message
...
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?





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Looking to select noncontiguous rows with For loop

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?



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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?




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Looking to select noncontiguous rows with For loop

this woild be very fast anyway, but you might add
Application.ScreenUpdating = False
before the loop



"Rick Rothstein" wrote in message
...
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?



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Looking to select noncontiguous rows with For loop

Yes, good idea (and, of course, setting it back to True again at the end of
the loop)... seems like when I post at 4:30 in the morning just before going
to sleep, I tend to forget things like this... thanks for catching this.

--
Rick (MVP - Excel)


"Patrick Molloy" wrote in message
...
this woild be very fast anyway, but you might add
Application.ScreenUpdating = False
before the loop



"Rick Rothstein" wrote in message
...
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?




  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Looking to select noncontiguous rows with For loop

Thank you both very much for your help. I've posted my code below to show
you what I ended up going with. Works as intented. My code:

Sub DirectorFormat()

Dim TSLastPFRow As Integer 'Tally Sheet
Dim TSPFTotal As Integer 'Tally Sheet PF
Dim ZeroRow As Long, i As Long

With Sheets("Tally Sheet")
.Cells.Copy
.Paste Destination:=Worksheets("DirectorCopy").Range("A1" )
End With

With Worksheets("DirectorCopy")
.Shapes("LazyEyeButton").Cut
For j = 1 To 64
.Shapes("Done! " & j).Cut
Next
.Columns("G:G").Delete
.Cells.Copy
.Cells.PasteSpecial Paste:=xlPasteValues,
Operation:=xlPasteSpecialOperationNone, _
SkipBlanks:=False, Transpose:=False
'Find the last PF
For i = 4 To Rows.Count Step 8
If Cells(i, "A").Value = 0 Then
ZeroRow = i
Exit For
End If
Next
TSLastPFRow = ZeroRow - 9
TSPFTotal = (Val(Replace(Cells(TSLastPFRow, 1).Value, "_PF", "")))
.Rows(ZeroRow & ":515").Delete
For i = (ZeroRow - 7) To 13 Step -8
.Rows(i).Delete
Next
.Rows("6:6").Select
ActiveWindow.FreezePanes = True
End With
End Sub

BTW, the line

..Rows(ZeroRow & ":515").Delete

seems to hang for some reason. Any idea why deleting rows would do this?
Is there a more effective way to code it?

"Patrick Molloy" wrote:

this woild be very fast anyway, but you might add
Application.ScreenUpdating = False
before the loop



"Rick Rothstein" wrote in message
...
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?






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select noncontiguous multiple columns by column number? c mateland Excel Programming 8 October 22nd 06 12:23 PM
Noncontiguous rows KC8DCN Excel Discussion (Misc queries) 3 August 18th 06 05:34 AM
Specifying Range.Select in a loop MervB Excel Programming 5 November 10th 05 12:41 AM
How can I print noncontiguous rows on the same page? Mary A Walthall Excel Discussion (Misc queries) 1 April 26th 05 10:00 AM
How to select columns in loop Tom Ogilvy Excel Programming 1 January 18th 05 05:53 PM


All times are GMT +1. The time now is 03:44 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"