Consulting

Results 1 to 4 of 4

Thread: Change tab colour when condition is met

  1. #1
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location

    Change tab colour when condition is met

    Hello
    I am trying to set up a workbook for different bills to be paid with an alert system for forthcoming dates. I have attached an example workbook that has conditional formatting set up on worksheet that I am happy with. What I need help with is the code on the worksheet tab to change colour after certain conditions are met.
    For example there is a due date in C4 with D6 being plus 30 days. If D6 is greater than 30 then I want the tab to be green, less than 30 orange, -30 red. The code I am tinkering with seems to run through but does not change the tab colour. At present it is only the active cell that I have set but I would like it to be the range D612. All help or comment would be appreciated

    Sorry, I forgot to include the bit of code I was trying
    [VBA]Sub Auto_Open()

    Dim ws As Worksheet
    Dim wb As Workbook

    Set wb = ThisWorkbook

    For Each ws In wb.Worksheets
    Range("D6").Select
    If ActiveCell.FormulaR1C1 = "=Value>(RC[-1]+30)" Then
    ActiveSheet.Tab.ColorIndex = 3
    ElseIf ActiveCell.FormulaR1C1 = "=Value(RC[-1]+30)" Then
    ActiveSheet.Tab.ColorIndex = 46
    ElseIf ActiveCell.FormulaR1C1 = "=Value<(RC[-1]-30)" Then
    ActiveSheet.Tab.ColorIndex = 4
    End If
    Next ws
    End Sub[/VBA]
    Attached Files Attached Files
    Last edited by Gil; 04-07-2013 at 01:11 PM.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Best guess as to your requirements
    [VBA]Sub Auto_Open()

    Dim ws As Worksheet
    Dim wb As Workbook
    Dim cel As Range, x As Long, y As Long

    Set wb = ThisWorkbook

    For Each ws In wb.Worksheets
    x = 60
    'Find minimum value
    For Each cel In ws.Range("C6").Resize(7)
    x = Application.Min(x, (cel.Offset(, 1) - cel))
    Next

    'Allocate colour
    Select Case x
    Case Is >= 30
    ws.Tab.ColorIndex = 4
    Case Is > 0
    ws.Tab.ColorIndex = 46
    Case Is < 0
    ws.Tab.ColorIndex = 3
    End Select
    Next ws
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location
    Thank you mdmckillop

    The code you have supplied doesn't seem to work past the Case Is >=30. I have figured out what most bits of the code refer to but what does x=60 mean. Also when the WB is opened the code does not auto run.

    Thanks for your continued support Gil

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Auto_Open goes into a standard module.

    You say you want to inspect D6 12. That is what the Find Minimum does. To find a minimum, I need to start somewhere, so I set X to 60, a value bigger than those you are looking to compare.
    The Select Case may need some fine tuning to suit your requirements, I'll leave that to you.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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