Macro For Tab Colors if Value is Greater Than or Less Than

davcurnutt

New Member
Joined
Mar 31, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I have a large spreadsheet and I want to turn tabs green if a value in a specified column exceeds 0.01 and yellow if a value is less than -0.05, otherwise no color. This is my current macro for green and white. How can I add the yellow?

Sub Tabcolor()
'
' Tabcolor Macro
'
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
If Application.WorksheetFunction.CountIf(ws.Range("O1:O900"), ">0.01") > 0 Then
ws.Tab.Color = vbGreen
Else
ws.Tab.Color = vbWhite
End If
Next ws


'
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try the following on a copy of your workbook.

VBA Code:
Sub Tabcolor()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If Application.WorksheetFunction.CountIf(ws.Range("O1:O900"), ">0.01") > 0 Then
            ws.Tab.Color = vbGreen
        ElseIf Application.WorksheetFunction.CountIf(ws.Range("O1:O900"), ">-0.05") > 0 Then
            ws.Tab.Color = vbYellow
        Else
            ws.Tab.Color = vbWhite
        End If
    Next ws
End Sub
 
Upvote 0
Try the following on a copy of your workbook.

VBA Code:
Sub Tabcolor()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If Application.WorksheetFunction.CountIf(ws.Range("O1:O900"), ">0.01") > 0 Then
            ws.Tab.Color = vbGreen
        ElseIf Application.WorksheetFunction.CountIf(ws.Range("O1:O900"), ">-0.05") > 0 Then
            ws.Tab.Color = vbYellow
        Else
            ws.Tab.Color = vbWhite
        End If
    Next ws
End Sub
Awesome. Thank you. The only thing I changed was
ElseIf Application.WorksheetFunction.CountIf(ws.Range("O1:O900"), ">-0.05") > 0 Then to
ElseIf Application.WorksheetFunction.CountIf(ws.Range("O1:O900"), "<-0.05") > 0 Then
Thank you
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,659
Members
449,114
Latest member
aides

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