+ Reply to Thread
Results 1 to 3 of 3

Loop sheets that has color

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Loop sheets that has color

    Hello,

    For some reason, it is not looping the sheets.

    As you can see below, what I want is
    • Loop through all sheets
    • if sheet color is 'green' which is indicated by color index 5296274 then check
    • if sheet name is not equal to "Consol" then protect the worksheet
    • else, input formulas on I2 to I4 and then protect the sheet.

    For some reason, when I try to check it, it's only looping 1 sheet, which through the msgbox, returns "Sheet1 Color: False"

    Any idea what did I do wrong?

    For Each sht In ThisWorkbook.Sheets
        shtColor = sht.Tab.Color
        shtName = sht.Name
        MsgBox shtName & " Color: " & shtColor
        If sht.Tab.Color = 5296274 Then
            Sheets(sht).Activate
            If sht.Name <> "Consol" Then
                With sht
                    .EnableSelection = xlNoSelection
                    .Protect = "abc"
                End With
            Else
                With sht
                    .Range("I2").Select
                    .Range("I2").Formula = "='" & sht.Name & " input tab'!I6"
                    .Range("I3").Select
                    .Range("I3").Formula = "='" & sht.Name & " input tab'!I7"
                    .Range("I4").Select
                    .Range("I4").Formula = "='" & sht.Name & " input tab'!I8"
                    .EnableSelection = xlNoSelection
                    .Protect = "abc"
                End With
            End If
        End If
    Next sht

  2. #2
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Loop sheets that has color

    I tried to define shtColor as Long. Otherwise a Boolean value seems to be stored. I don't know why.

    Sub Test()
        Dim shtColor As Long
        Dim sht As Worksheet
        
        For Each sht In ThisWorkbook.Worksheets
            shtColor = sht.Tab.Color
            shtName = sht.Name
            MsgBox shtName & " Color: " & shtColor
            If sht.Tab.Color = 5296274 Then
                sht.Activate
                If sht.Name <> "Consol" Then
                    With sht
                        .EnableSelection = xlNoSelection
                        .Protect Password:="abc"
                    End With
                Else
                    With sht
                        .Range("I2").Select
                        .Range("I2").Formula = "='" & sht.Name & " input tab'!I6"
                        .Range("I3").Select
                        .Range("I3").Formula = "='" & sht.Name & " input tab'!I7"
                        .Range("I4").Select
                        .Range("I4").Formula = "='" & sht.Name & " input tab'!I8"
                        .EnableSelection = xlNoSelection
                        .Protect Password:="abc"
                    End With
                End If
            End If
        Next sht
    End Sub
    1. Reply to thread and inform if suggestion was helpful or not
    2. Click on the star (=Add Reputation) if you think someone helped you
    3. Mark [SOLVED] to this thread if solution was found. (On Menu "Thread Tools" > "Mark this thread as solved")

  3. #3
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: Loop sheets that has color

    Thanks for your help Joakim!

    I was playing around and found the solution.

    I just had to change

    ThisWorkbook.Sheets
    to

    WorkSheets

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Loop worksheets and if sheets Name matches pattern, rename sheets
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-23-2016, 01:08 AM
  2. Loop which changes color of row if x>y
    By yeaho26 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2015, 10:36 AM
  3. [SOLVED] Loop codes through multiple sheets (not all sheets in workbook)
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2014, 02:11 AM
  4. [SOLVED] loop action through all sheets in workbook skipping certain sheets
    By sawa85sa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2013, 06:40 PM
  5. Help with loop to fill color of shapes (US map)
    By harleyberger in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-28-2012, 01:50 PM
  6. Loop through data points and change color
    By harleyberger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-04-2012, 01:33 AM
  7. [SOLVED] Command Button Color For Next Loop
    By John Wilson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-15-2005, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1