Using VBA to change sheet tab color to a particular color if found in range.

dugweje

New Member
Joined
Feb 28, 2022
Messages
5
Office Version
  1. 2016
Hello. I have sheets that I download for my job that have a shade of red in a cell to indicate that it is over the limit. The range is A:1 to A:3000. I was trying to create a code that if any cell( even if just one) in that range is the shade of red the sheet tab would also turn into that shade of red. Thank you any help is appreciated.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
have a shade of red in a cell
a shade of red, does that mean a "fixed" red color, normally done in the 3 variabels of RGB, like RGB(250,10,10) = 1 value
or is it not that fixed.
in human talk, is it "that color red", without variation ?
In excel, you can have hundreds of colors "red" !
 
Upvote 0
VBA Code:
Option Explicit
Sub test()
Dim Lr&, cell As Range
Lr = Cells(Rows.Count, "A").End(xlUp).Row
    For Each cell In Range("A1:A" & Lr)
        ' If cell color is red with RGB code = 3, use this
        If cell.Interior.ColorIndex = 3 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
        ' If any color is found then tab color is set with same color, use this
        'If cell.Interior.ColorIndex >0 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
    Next
End Sub
Use: for red =3
If cell.Interior.ColorIndex = 3 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
Or any color found:
If cell.Interior.ColorIndex >0 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
 
Upvote 0
Solution
VBA Code:
Option Explicit
Sub test()
Dim Lr&, cell As Range
Lr = Cells(Rows.Count, "A").End(xlUp).Row
    For Each cell In Range("A1:A" & Lr)
        ' If cell color is red with RGB code = 3, use this
        If cell.Interior.ColorIndex = 3 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
        ' If any color is found then tab color is set with same color, use this
        'If cell.Interior.ColorIndex >0 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
    Next
End Sub
Use: for red =3
If cell.Interior.ColorIndex = 3 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
Or any color found:
If cell.Interior.ColorIndex >0 Then ActiveSheet.Tab.ColorIndex = cell.Interior.ColorIndex
This worked perfectly thank you so much
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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