Simple loop to change Tab colors for some, but not all tabs

choop

New Member
Joined
Oct 21, 2005
Messages
21
Hi All,

I'm looking for a short piece of code to do the following:

Loop through my workbook, and change the tab color if cell F26 is <>0. I have the code for updating the color ok, but i'm struggling with the loop.

Here are a few questions/assumptions as well as the code (throws out of memory error) I tried and failed with:
1) Can I write one function for the updating of color, and pass it the appropriate worksheet name? what would that the two functions need to look like?
2) My list of worksheets that I want to ignore are in a variable called Ignore_Sheets with about 15 integers representing the sheet numbers to ignore

Sub WorksheetLoop()

Dim WS_Count As Integer
Dim I As Integer
Dim Ignore_Sheets(10, 13, 15, 2, 28, 33, 4, 49, 5, 68, 81, 83, 87, 88, 90)
IgnoreCount = UBound(Ignore_Sheets)
WS_Count = ActiveWorkbook.Worksheets.Count

For I = 1 To WS_Count
For J = 1 To IgnoreCount
If IgnoreCount.J = I Then Exit For
Next

With ActiveWorkbook.Worksheets(I).Name
MyVal = Range("f26")

Select Case MyVal
Case Is = 0
.Color = vbGreen
Case -1.5 To 1.5
.Color = vbYellow
Case Else
.Color = vbRed
End Select
End With

Next
End Sub

Thanks for the help in advance!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Are the numbers in the ignore sheets, the index number, or the actual sheet name?
 
Upvote 0
Ok, those are the sheet code names, how about
VBA Code:
Sub choop()
   Dim Ws As Worksheet

   For Each Ws In Worksheets
      Select Case Ws.CodeName
         Case "Sheet10", "Sheet13", "Sheet2"
         Case Else
            Select Case Ws.Range("F26").Value
               Case 0: Ws.Tab.Color = vbGreen
               Case -1.5 To 1.5: Ws.Tab.Color = vbYellow
               Case Else: Ws.Tab.Color = vbRed
            End Select
      End Select
   Next Ws
End Sub
Just add the rest of the codenames
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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