Consulting

Results 1 to 6 of 6

Thread: Change sheet tab color automatically based on cell values

  1. #1
    VBAX Newbie
    Joined
    Jun 2021
    Posts
    3
    Location

    Exclamation Change sheet tab color automatically based on cell values

    Hi All! This is my first post - I am excited to be part of the community.
    I have an Excel workbook in which I update values daily and if at anytime a zero value is entered in the values column, I would like the tab color for that sheet to turn red. I'd like to copy that code for all sheets in the workbook. I have been trying this code but can't get it to work:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Select Case Sheets.Range("$D").Value
             Case Is = 0
                 Sheets.Tab.Color = vbRed
             End Select
    End Sub
    Any input you can provide would be greatly appreciated!!
    Thanks!
    Last edited by Paul_Hossler; 06-01-2021 at 11:05 AM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. Welcome to the forum. Take a few minutes and read the FAQs at the link in my sig

    2. I added CODE tags to your macro, you can use the [#] icon next time

    3. You had a few errors

    4. You didn't say what you want to do if the 0's are eventually replaced. Tab goes red with a 0 is entered, stays red for more 0's, but what happens when the 0's are all gone?

    Try this

    Option Explicit
    
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        If Application.WorksheetFunction.CountIf(Sh.Columns(4), 0) = 0 Then
            Sh.Tab.Color = vbGreen
        Else
            Sh.Tab.Color = vbRed
        End If
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 06-01-2021 at 11:40 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Jun 2021
    Posts
    3
    Location
    Thank you so much for the quick response Paul!
    I have read the FAQ's thank you for pointing me to those.
    If the 0's are eventually replaced by another value, the red tab would go away.
    I added your code but am still doing something wrong apparently.
    I've included a sample of my file with the code added.
    For example, the "Roberts 1-3" worksheet has zero values and I would like that tab to be red.

    Thank you again for all your help hopefully I can figure this out.
    Attached Files Attached Files

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I edited my first reply

    I like Green

    Try this
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Newbie
    Joined
    Jun 2021
    Posts
    3
    Location
    Oh wow thank you that worked great!!

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Glad it worked for you

    It can be made a little more robust and elegant

    #3 in my sig explains how you can mark it [SOLVED]

    Option Explicit
    
    
    Const ColorNoZeros As Long = vbGreen
    Const ColorSomeZeros As Long = vbRed
    
    
    Private Sub Workbook_Open()
        Dim ws As Worksheet
        
        Application.ScreenUpdating = False
        
        For Each ws In ThisWorkbook.Worksheets
            Call pvtChangeTabColor(ws)
        Next
    
    
        Application.ScreenUpdating = True
    End Sub
    
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        Call pvtChangeTabColor(Sh)
    End Sub
    
    
    
    
    Private Sub pvtChangeTabColor(ws As Worksheet)
        With ws
            If UCase(Trim(.Range("D1").Value)) <> "MCF" Then Exit Sub
                
            If Application.WorksheetFunction.CountIf(.Columns(4), 0) = 0 Then
                .Tab.Color = ColorNoZeros
            Else
                .Tab.Color = ColorSomeZeros
            End If
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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