#1   Report Post  
Helen
 
Posts: n/a
Default macro loop

I have worksheets with several hundred rows and want to record a macro that
will highlight every other row, starting at row 2, until the end. I can
record a macro to highlight several rows but how can I repeat until the end
of the worksheet?
  #2   Report Post  
Bob Phillips
 
Posts: n/a
Default


cLastRow = Cells(Rows.Count,"A").End(xlUp).Row
For i = 1 to cLastRow Step 2
cells((i,"A").Entirerow.ColorIndex = 35
Next i

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Helen" wrote in message
...
I have worksheets with several hundred rows and want to record a macro

that
will highlight every other row, starting at row 2, until the end. I can
record a macro to highlight several rows but how can I repeat until the

end
of the worksheet?



  #3   Report Post  
Jason Morin
 
Posts: n/a
Default

Try:

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim cell As Range

Application.ScreenUpdating = False
iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each cell In colorRng
With cell
If .Row Mod 2 = 0 Then
.EntireRow.Interior.ColorIndex = 3 'Red
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub

---
To use this macro, press ALT+F11, go to Insert Module,
and paste in the code in the window. Go back to XL (ALT+Q)
and run the macro under Tools Macro Macros.

HTH
Jason
Atlanta, GA



-----Original Message-----
I have worksheets with several hundred rows and want to

record a macro that
will highlight every other row, starting at row 2, until

the end. I can
record a macro to highlight several rows but how can I

repeat until the end
of the worksheet?
.

  #4   Report Post  
Bob Phillips
 
Posts: n/a
Default

Hi Jason,

Your code has an built-in redundancy, in that in goes through each cell in
the range and setting the colour of that whole row if it meets the test
criteria. But as you set your range to the whole rows, the loop iterates
through each cell in the range, A2, B2, C2, etc. Thus A2 is tested and the
row is coloured, B2 is tested and the same row is coloured, C2 is tested and
the same row is coloured, etc.

This can be circumvented by changing

Set colorRng = Range("2:" & iLastRow)

to

Set colorRng = Range("A2:A" & iLastRow)


Regards

Bob

"Jason Morin" wrote in message
...
Try:

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim cell As Range

Application.ScreenUpdating = False
iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each cell In colorRng
With cell
If .Row Mod 2 = 0 Then
.EntireRow.Interior.ColorIndex = 3 'Red
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub

---
To use this macro, press ALT+F11, go to Insert Module,
and paste in the code in the window. Go back to XL (ALT+Q)
and run the macro under Tools Macro Macros.

HTH
Jason
Atlanta, GA



-----Original Message-----
I have worksheets with several hundred rows and want to

record a macro that
will highlight every other row, starting at row 2, until

the end. I can
record a macro to highlight several rows but how can I

repeat until the end
of the worksheet?
.



  #5   Report Post  
Jason Morin
 
Posts: n/a
Default

Yep, you're right, Bob. I started to think about that
after I posted and realized that I didn't have to loop
through every cell of every row, only those cells in col.
A. I had "row" on the brain.

Thanks.

Jason


-----Original Message-----
Hi Jason,

Your code has an built-in redundancy, in that in goes

through each cell in
the range and setting the colour of that whole row if it

meets the test
criteria. But as you set your range to the whole rows,

the loop iterates
through each cell in the range, A2, B2, C2, etc. Thus A2

is tested and the
row is coloured, B2 is tested and the same row is

coloured, C2 is tested and
the same row is coloured, etc.

This can be circumvented by changing

Set colorRng = Range("2:" & iLastRow)

to

Set colorRng = Range("A2:A" & iLastRow)


Regards

Bob

"Jason Morin" wrote

in message
...
Try:

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim cell As Range

Application.ScreenUpdating = False
iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each cell In colorRng
With cell
If .Row Mod 2 = 0 Then
.EntireRow.Interior.ColorIndex = 3 'Red
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub

---
To use this macro, press ALT+F11, go to Insert

Module,
and paste in the code in the window. Go back to XL

(ALT+Q)
and run the macro under Tools Macro Macros.

HTH
Jason
Atlanta, GA



-----Original Message-----
I have worksheets with several hundred rows and want

to
record a macro that
will highlight every other row, starting at row 2,

until
the end. I can
record a macro to highlight several rows but how can I

repeat until the end
of the worksheet?
.



.



  #6   Report Post  
Dave Peterson
 
Posts: n/a
Default

One simple change and you'd be ok, though:

Change:
For Each cell In colorRng
to
For Each cell In colorRng.Rows

Now even though your variable names (Cell) aren't very descriptive, it'll work.

(Cell would be a Row (if that makes sense???))

Mass changing Cell to MyRow (only for readability):

Option Explicit

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim myRow As Range

Application.ScreenUpdating = False
iLastRow = cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each myRow In colorRng.Rows
With myRow
If .Row Mod 2 = 0 Then
.EntireRow.Interior.ColorIndex = 3 'Red
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub






Jason Morin wrote:

Yep, you're right, Bob. I started to think about that
after I posted and realized that I didn't have to loop
through every cell of every row, only those cells in col.
A. I had "row" on the brain.

Thanks.

Jason

-----Original Message-----
Hi Jason,

Your code has an built-in redundancy, in that in goes

through each cell in
the range and setting the colour of that whole row if it

meets the test
criteria. But as you set your range to the whole rows,

the loop iterates
through each cell in the range, A2, B2, C2, etc. Thus A2

is tested and the
row is coloured, B2 is tested and the same row is

coloured, C2 is tested and
the same row is coloured, etc.

This can be circumvented by changing

Set colorRng = Range("2:" & iLastRow)

to

Set colorRng = Range("A2:A" & iLastRow)


Regards

Bob

"Jason Morin" wrote

in message
...
Try:

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim cell As Range

Application.ScreenUpdating = False
iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each cell In colorRng
With cell
If .Row Mod 2 = 0 Then
.EntireRow.Interior.ColorIndex = 3 'Red
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub

---
To use this macro, press ALT+F11, go to Insert

Module,
and paste in the code in the window. Go back to XL

(ALT+Q)
and run the macro under Tools Macro Macros.

HTH
Jason
Atlanta, GA



-----Original Message-----
I have worksheets with several hundred rows and want

to
record a macro that
will highlight every other row, starting at row 2,

until
the end. I can
record a macro to highlight several rows but how can I
repeat until the end
of the worksheet?
.



.


--

Dave Peterson
  #7   Report Post  
Bob Phillips
 
Posts: n/a
Default

If you are going to go this way, you should take out the EntireRow property,
as myRow is an entirerow

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim myRow As Range

Application.ScreenUpdating = False
iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each myRow In colorRng.Rows
With myRow
If .Row Mod 2 = 0 Then
.Interior.ColorIndex = 3 'Red '<<<<<<<<<<<<<<<<
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dave Peterson" wrote in message
...
One simple change and you'd be ok, though:

Change:
For Each cell In colorRng
to
For Each cell In colorRng.Rows

Now even though your variable names (Cell) aren't very descriptive, it'll

work.

(Cell would be a Row (if that makes sense???))

Mass changing Cell to MyRow (only for readability):

Option Explicit

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim myRow As Range

Application.ScreenUpdating = False
iLastRow = cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each myRow In colorRng.Rows
With myRow
If .Row Mod 2 = 0 Then
.EntireRow.Interior.ColorIndex = 3 'Red
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub






Jason Morin wrote:

Yep, you're right, Bob. I started to think about that
after I posted and realized that I didn't have to loop
through every cell of every row, only those cells in col.
A. I had "row" on the brain.

Thanks.

Jason

-----Original Message-----
Hi Jason,

Your code has an built-in redundancy, in that in goes

through each cell in
the range and setting the colour of that whole row if it

meets the test
criteria. But as you set your range to the whole rows,

the loop iterates
through each cell in the range, A2, B2, C2, etc. Thus A2

is tested and the
row is coloured, B2 is tested and the same row is

coloured, C2 is tested and
the same row is coloured, etc.

This can be circumvented by changing

Set colorRng = Range("2:" & iLastRow)

to

Set colorRng = Range("A2:A" & iLastRow)


Regards

Bob

"Jason Morin" wrote

in message
...
Try:

Sub ColorRows()

Dim iLastRow As Long
Dim colorRng As Range
Dim cell As Range

Application.ScreenUpdating = False
iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set colorRng = Range("2:" & iLastRow)
For Each cell In colorRng
With cell
If .Row Mod 2 = 0 Then
.EntireRow.Interior.ColorIndex = 3 'Red
'Blue=5,Green=10,Yellow=6
End If
End With
Next
Application.ScreenUpdating = True
End Sub

---
To use this macro, press ALT+F11, go to Insert

Module,
and paste in the code in the window. Go back to XL

(ALT+Q)
and run the macro under Tools Macro Macros.

HTH
Jason
Atlanta, GA



-----Original Message-----
I have worksheets with several hundred rows and want

to
record a macro that
will highlight every other row, starting at row 2,

until
the end. I can
record a macro to highlight several rows but how can I
repeat until the end
of the worksheet?
.



.


--

Dave Peterson



  #8   Report Post  
Bill Martin -- (Remove NOSPAM from address)
 
Posts: n/a
Default

Helen wrote:
I have worksheets with several hundred rows and want to record a macro that
will highlight every other row, starting at row 2, until the end. I can
record a macro to highlight several rows but how can I repeat until the end
of the worksheet?


To state more directly what the other guys have been talking about, you
can't really *record* a macro with a loop. What you can do is record
the inner loop of the macro, and then go back and manually edit the
macro to add the loop around that code.

Frequently I use a While/Wend loop for this purpose. I start the macro
with a cell selected and "While" that cell is greater than 0 I execute
the loop and move the selected cell down one row.

This works nicely to build a table for a chart for example. I create a
column of X values. The macro plugs them one at a time into the
spreadsheet, then fetches the result to the table and increments the
selected cell down one row.

Good luck...

Bill
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
Macro - - Automation Jac Excel Discussion (Misc queries) 8 December 27th 04 02:42 PM
Record Macro Relative does not work? lbbss Excel Discussion (Misc queries) 3 December 13th 04 08:43 PM
Record Macro Relative does not work? lbbss Excel Discussion (Misc queries) 1 December 13th 04 07:55 PM
Macro and If Statement SATB Excel Discussion (Misc queries) 2 December 3rd 04 04:46 PM
VBA Newbie: Help with Do Loop code Carl Excel Discussion (Misc queries) 3 December 2nd 04 07:04 PM


All times are GMT +1. The time now is 04:11 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"