Change Tab Color based on dynamic cell value

crazi4sports10

New Member
Joined
Jul 22, 2015
Messages
8
I need to change the tab color based on the value found under the words "inception difference" in that tab. The tabs have between 1-10 numbers below "inception difference." If all numbers below "inception difference" are negative, make the tab red. If they are all positive, make the tab green. If some are negative and some are positive, make the tab yellow.
 
Is there always at least one blank cell below "inception difference"?
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Code:
Sub InceptionDifference()

Dim LastRow As Long
Dim LastColumn As Integer
Dim id As Range
Dim sht As Worksheet
Dim i As Long
Dim r As Range

For i = 1 To ThisWorkbook.Worksheets.Count
    Worksheets(i).Activate
    Set r = Nothing
    Set id = Nothing
    
    If WorksheetFunction.CountA(Cells) > 0 Then
        LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    
        Set r = Range(Cells(1, 1), Cells(LastRow, LastColumn))
        Set id = r.Find(What:="inception difference", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
            
        Dim idLastRow As Long
        Dim idFirstRow As Long
        Dim r2 As Range
        Set r2 = Nothing
    
    If id.Value <> "" Then
        If id.Offset(1, 0) <> "" Then
            idFirstRow = id.Offset(1, 0).Row
            idLastRow = id.End(xlDown).Row
        Else
            idFirstRow = id.End(xlDown).Row
            idLastRow = id.End(xlDown).End(xlDown).Row
        End If

    Set r2 = Range(Cells(idFirstRow, id.Column), Cells(idLastRow, id.Column))

        If Application.CountIf(r2, "<0") = r2.Count Then
            ActiveWorkbook.Sheets(i).Tab.Color = 255
        Else
            If Application.CountIf(r2, ">0") = r2.Count Then
                ActiveWorkbook.Sheets(i).Tab.Color = 5287936
            Else
                ActiveWorkbook.Sheets(i).Tab.Color = 65535
                End If
            End If
        End If
    End If
Next i
    
End Sub

The code may not be pretty - some may even call it ugly - but I think it works...

Cheers,

tonyyy
 
Upvote 0
Although I am no expert, I understand and agree with the rationale in the code. Unfortunately, not there yet - the Macro with your most recent code gives "Run-time error '91': Object variable or With block variable not set" - when I try to debug the error, it highlights 'If id.Value <> "" Then' - other thoughts?
 
Upvote 0
Try replacing the error line with...

Code:
If Not id Is Nothing Then
 
Upvote 0
Golden!! Works like a charm - one more small request. IF one of the tabs has "inception difference" and then doesn't have a value under it, can you highlight it black so I know to address the missing data? Otherwise it's good to go!
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,931
Members
449,480
Latest member
yesitisasport

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