2

I am trying to create a button that upon one click, will check a specific cell in 10 sheets of a workbook, and recolour the tabs depending on a cell value.

For example,

  • If cell E15 > 18, then the tab colour should turn green.
  • If cell E15 < 18, then the tab colour should turn red.

All 10 tabs should be evaluated and recoloured upon a single button click.

So far my macro looks like this, giving just three sheets for an example. It's very crude but I am very new to VBA (1 day).

My main issue is that it works for the first tab, but then opens the second tab and says "Object Required"

Sub Update_Tab_Colour_One_Click()    
    Sheets(4).Activate    
    If Cells(13, 11).Value > 18 Then
        With ActiveWorkbook.ActiveSheet.Tab
            .Color = vbGreen
        End With
    Else
        With ActiveWorbook.ActiveSheet.Tab
            .Color = vbRed
        End With
    End If    

    Sheets(5).Activate    
    If Cells(13, 11).Value > 18 Then
        With ActiveWorkbook.ActiveSheet.Tab
            .Color = vbGreen
        End With
    Else
        With ActiveWorbook.ActiveSheet.Tab
            .Color = vbRed
        End With
    End If

    Sheets(6).Activate    
    If Cells(13, 11).Value > 18 Then
        With ActiveWorkbook.ActiveSheet.Tab
            .Color = vbGreen
        End With
    Else
        With ActiveWorbook.ActiveSheet.Tab
            .Color = vbRed
        End With
    End If    
End Sub
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • what line do you get an error on? – 99moorem Aug 21 '17 at 14:59
  • 2
    Avoid using ActiveSheet. You can also remove the Activate statements that way. – davidmneedham Aug 21 '17 at 15:01
  • Avoid activating the sheets and just use a loop. What's your error? – David Zemens Aug 21 '17 at 15:01
  • 1
    Good first post! As you've noted, the answers below all work. Could you select one of them as The Answer by clicking the check mark left of the post you want to mark as "the answer" (it's below the up/down arrows). This also helps because when browsing, it'll show the question as Answered/closed for other folks. (Personally, I like @DavidZemens' answer, but it's up to you of course!) – BruceWayne Aug 21 '17 at 15:51

3 Answers3

4

See if this works for you:

Sub Update_Tab_Colour_One_Click()
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    If ws.Index = 4 Or ws.Index = 5 Or ws.Index = 6 Then
        If ws.Cells(13, 11).Value > 18 Then
            ws.Tab.Color = vbGreen
        Else
            ws.Tab.Color = vbRed
        End If
    End If
Next ws

End Sub

It checks to see if it's the 4, 5, or 6th indexed sheet, then checks the cell value and colors the tab accordingly.

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • Hi BruceWayne (fantastic name). That worked perfectly! I am so grateful for your instant response! Can't wait to know enough about VBA to be able to solve silly issues like this. If you don't mind, how did you learn VBA? – Joe Honeywood Aug 21 '17 at 15:06
  • 1
    @JoeHoneywood - Macro Recording, learning to [avoid using `.Select`/`.Activate`](https://stackoverflow.com/questions/10714251/) (which helps with learning to use variables), and lots of Google and absolutely couldn't do it without SO and the great folks here :D – BruceWayne Aug 21 '17 at 15:11
3

Something like this would probably do it.

Dim sh as Worksheet
Dim s as Long

For s = 4 to 13 ' Modify if needed
    Set sh = ThisWorkbook.Worksheets(s)
    With sh
        .Tab.Color = IIF(.Cells(13,11).Value > 18, vbGreen, vbRed)
    End With
Next

Here we've created a For/Next loop over the sheets indexed 4:13 (10 sheets, incremented by 1). Then, we set a Worksheet variable (sh) to represent the current sheet (note that it is not needed to be Active), then set the sh.Tab.Color based on the boolean expression in the IIF function to return either vbGreen or vbRed.

Info:

For..Next statement reference

IIF function reference

How to avoid using Select in Excel VBA

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • 1
    I always forget about `IIF()`, nice thinking with that and the `Tab.Color`. – BruceWayne Aug 21 '17 at 15:06
  • 2
    I'd bin off the creation of the `sh` variable, since it's only used once, and use `With ThisWorkbook.Sheets(s): .Tab.Color = IIF(.Cells(13,11).Value > 18, vbGreen, vbRed): End With` – Wolfie Aug 21 '17 at 15:08
  • This also works! Thank you so much. I appreciate the explanation as well and the link to the further documentation! Great help – Joe Honeywood Aug 21 '17 at 15:12
2

Looping over non-consecutive and named sheets

This alternative lets you loop over sheets which aren't consecutive (so 2,4,7 not just 1,2,3) and by their names (like "Sheet1", "Sheet2"). So it is much more flexible!

It happens to be just as short to write as a straight forward loop, we are just looping over an array of sheet names or numbers instead.

I have added comments to explain what each line does, see below:

Sub Update_Tab_Colour_One_Click()
    ' Declare array of sheet numbers and/or names
    Dim mySheets As Variant
    mySheets = Array(2, 4, "Sheet1")
    ' Loop over sheet numbers / names
    Dim i As Long
    For i = LBound(mySheets) To UBound(mySheets)
        ' Use With so we don't have to repeatedly say we are within this sheet
        With ThisWorkbook.Sheets(mySheets(i))
            ' Use IIF to concisely assign a conditional value
            .Tab.Color = IIf(.Cells(13, 11).Value > 18, vbGreen, vbRed)
        End With
    Next i
End Sub
Community
  • 1
  • 1
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • Another perfect answer that works. Joining this website was the best thing I've ever done! Really appreciate everyone's expertise. – Joe Honeywood Aug 21 '17 at 15:27
  • 1
    Glad to have helped! You got a good response because you gave a clear problem description and some example code you were having issues with. Your next question would be made even better by including which line throws the error ([debug line by line using F8](http://www.cpearson.com/excel/DebuggingVBA.aspx)). – Wolfie Aug 21 '17 at 15:30