for each sheet in worksheets

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi the code below does not work. Complaining about
Code:
xsheet.Tab.ColorIndex = i

I did similar idea with cells and everything was fine. Please see the second code. Thank you so much.
Code:
Sub mysheets()
    Dim xsheet As Worksheet
    Dim i As Integer
    i = 0
    For Each xsheet In Worksheets
        xsheet.Tab.ColorIndex = i
        i = i + 1
    Next xsheet
End Sub
Code:
Sub myselection3()
 Dim xcell As Range
 Dim i As Integer
 For Each xcell In Selection
    xcell.Value = i
    xcell.Interior.ColorIndex = i
    i = i + 1
Next xcell
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This worked for me...
Code:
Sub ColorTabs()
  Dim X As Long
  For X = 1 To Sheets.Count
    Sheets(X).Tab.ColorIndex = 1 + (X Mod 56)
  Next
End Sub
 
Upvote 0
Thank you all. Yes if I start with i=1 then I am ok. Not sure why? I did the same thing for Cells and it works even when I=0

Code:
Sub myselection3()
 Dim xcell As Range
 Dim i As Integer
 i = 0
 For Each xcell In Selection
    xcell.Value = i
    xcell.Interior.ColorIndex = i
    i = i + 1
Next xcell
End Sub

Thank you again
 
Upvote 0
It's a quirk. With the Interior it treats colorindex 0 as the same as xlcolorindexnone.
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

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