Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Loop a code thru several worksheets

I have a code that I have perfected to work, but I cannot find the right
way to have it loop thru several worksheets. My code is as follows:

Sub Update_Row_Colors()

Dim LRow As Integer
Dim LCell As String
Dim LColorCells As String
'Start at row 7
LRow = 7

'Update row colors for the first 1000 rows
While LRow < 1000
LCell = "H" & LRow
'Color will change in columns A to CV
LColorCells = "A" & LRow & ":" & "CV" & LRow

Select Case Left(Range(LCell).Value, 6)

'Set row color to yellow
Case "H"
Range(LColorCells).Interior.ColorIndex = 6
Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to red
Case "C"
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = 3
Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to gray
Case "S"
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = 15
Range(LColorCells).Interior.Pattern = xlSolid

'Default all other rows to no color
Case Else
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = xlNone

End Select

LRow = LRow + 1
Wend

Range("A1").Select

End Sub

and here's the code i found to loop it, but it only works on the first
worksheet.

Option Explicit

Sub LoopThroughSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets

'** Perform code here **

'E.g.
On Error Resume Next 'Will continue if an error results
ws.Range("A1") = ws.Name

'***********************

Next ws
End Sub

Any help would be much appreciated.





*** Sent via Developersdex http://www.developersdex.com ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
r r is offline
external usenet poster
 
Posts: 125
Default Loop a code thru several worksheets

Sub LoopThroughSheets()
Dim ws As Worksheet
Dim sh As Worksheet
Set sh = ActiveSheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets

ws.Select
Call Update_Row_Colors
'** Perform code here **

'E.g.
On Error Resume Next 'Will continue if an error results
ws.Range("A1") = ws.Name

'***********************

Next ws
sh.Select
Application.ScreenUpdating = True
End Sub

regards
r

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


"Melanie" wrote:

I have a code that I have perfected to work, but I cannot find the right
way to have it loop thru several worksheets. My code is as follows:

Sub Update_Row_Colors()

Dim LRow As Integer
Dim LCell As String
Dim LColorCells As String
'Start at row 7
LRow = 7

'Update row colors for the first 1000 rows
While LRow < 1000
LCell = "H" & LRow
'Color will change in columns A to CV
LColorCells = "A" & LRow & ":" & "CV" & LRow

Select Case Left(Range(LCell).Value, 6)

'Set row color to yellow
Case "H"
Range(LColorCells).Interior.ColorIndex = 6
Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to red
Case "C"
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = 3
Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to gray
Case "S"
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = 15
Range(LColorCells).Interior.Pattern = xlSolid

'Default all other rows to no color
Case Else
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = xlNone

End Select

LRow = LRow + 1
Wend

Range("A1").Select

End Sub

and here's the code i found to loop it, but it only works on the first
worksheet.

Option Explicit

Sub LoopThroughSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets

'** Perform code here **

'E.g.
On Error Resume Next 'Will continue if an error results
ws.Range("A1") = ws.Name

'***********************

Next ws
End Sub

Any help would be much appreciated.





*** Sent via Developersdex http://www.developersdex.com ***

  #3   Report Post  
Posted to microsoft.public.excel.programming
r r is offline
external usenet poster
 
Posts: 125
Default Loop a code thru several worksheets

or ...

Sub Update_Row_Colors(SH As Worksheet)

Dim LRow As Integer
Dim LCell As String
Dim LColorCells As String
'Start at row 7
LRow = 7

'Update row colors for the first 1000 rows
With SH
While LRow < 1000
LCell = "H" & LRow
'Color will change in columns A to CV
LColorCells = "A" & LRow & ":" & "CV" & LRow

Select Case Left(.Range(LCell).Value, 6)
'Set row color to yellow
Case "H"
.Range(LColorCells).Interior.ColorIndex = 6
.Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to red
Case "C"
'.Rows(LRow & ":" & LRow).Select
.Range(LColorCells).Interior.ColorIndex = 3
.Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to gray
Case "S"
'.Rows(LRow & ":" & LRow).Select
.Range(LColorCells).Interior.ColorIndex = 15
.Range(LColorCells).Interior.Pattern = xlSolid

'Default all other rows to no color
Case Else
'.Rows(LRow & ":" & LRow).Select
.Range(LColorCells).Interior.ColorIndex = xlNone

End Select

LRow = LRow + 1
Wend

End With
End Sub




Sub LoopThroughSheets()
Dim ws As Worksheet
Dim SH As Worksheet
Set SH = ActiveSheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets

Update_Row_Colors ws
'** Perform code here **

'E.g.
On Error Resume Next 'Will continue if an error results
ws.Range("A1") = ws.Name

'***********************

Next ws
SH.Select
Application.ScreenUpdating = True
End Sub

regards
r


--
Come e dove incollare il codice:
http://www.rondebruin.nl/code.htm

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


"Melanie" wrote:

I have a code that I have perfected to work, but I cannot find the right
way to have it loop thru several worksheets. My code is as follows:

Sub Update_Row_Colors()

Dim LRow As Integer
Dim LCell As String
Dim LColorCells As String
'Start at row 7
LRow = 7

'Update row colors for the first 1000 rows
While LRow < 1000
LCell = "H" & LRow
'Color will change in columns A to CV
LColorCells = "A" & LRow & ":" & "CV" & LRow

Select Case Left(Range(LCell).Value, 6)

'Set row color to yellow
Case "H"
Range(LColorCells).Interior.ColorIndex = 6
Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to red
Case "C"
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = 3
Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to gray
Case "S"
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = 15
Range(LColorCells).Interior.Pattern = xlSolid

'Default all other rows to no color
Case Else
Rows(LRow & ":" & LRow).Select
Range(LColorCells).Interior.ColorIndex = xlNone

End Select

LRow = LRow + 1
Wend

Range("A1").Select

End Sub

and here's the code i found to loop it, but it only works on the first
worksheet.

Option Explicit

Sub LoopThroughSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets

'** Perform code here **

'E.g.
On Error Resume Next 'Will continue if an error results
ws.Range("A1") = ws.Name

'***********************

Next ws
End Sub

Any help would be much appreciated.





*** Sent via Developersdex http://www.developersdex.com ***

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
For/Next Loop through worksheets klysell Excel Programming 7 August 7th 07 07:40 PM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 5 March 29th 07 05:48 AM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 PM
(Complex) Loop within loop to create worksheets klysell Excel Programming 1 March 20th 07 12:03 AM
Loop Through worksheets Stephen[_9_] Excel Programming 5 April 20th 04 01:46 PM


All times are GMT +1. The time now is 12:14 AM.

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

About Us

"It's about Microsoft Excel"