excel vba change cell color based on tabs color

kelvin_9

Active Member
Joined
Mar 6, 2015
Messages
444
Office Version
  1. 2019
Hello All,

assuming i have 20 data in range A2:A21 at sheet 1 and they are same as the tabs name
i need a vba to return same color index (3, 5 and 6) in column A if tabs color was changed

example: when tabs name(220408120732319399) was changed to colorindex 5, i want cell A9(220408120732319399) change to colorindex 5 as well

thank you very much
 
Try:
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim fnd As Range
    For Each ws In Sheets
        If IsNumeric(ws.Name) Then
            Set fnd = Sheets("SUMMARY").Range("A:A").Find(ws.Name)
            If Not fnd Is Nothing Then
                fnd.Interior.Color = Sheets(ws.Name).Tab.Color
            End If
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
The macro assumes that the sheet names will consist only of numbers.
Hi mumps
thank you very much for your reply

THIS IS IMPECCABLE
i found sheet "summary" return 4 color which is exactly correct
blue and purple as tabs color
black as default which is no color
no filled as no founding

thank you very much
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi mumps

is this code can be amended if tab(s) didnt changed it's color, keep sheet "summary"
You are very welcome. :)
Hi mumps
can i use black color as no founding and keep it as default(no color) instead?

thank you very much again
 
Upvote 0
I'm sorry but I don't quite understand what you mean. Please clarify in detail using a few examples.
 
Upvote 0
I'm sorry but I don't quite understand what you mean. Please clarify in detail using a few examples.
Hi mumps
thank you very much for your reply

sorry for my poor language

example as below picture, row54 - row63
you will see row56, row57, row 61 or row 63... colored as tab color
but you will see row55, row58 or row62 is black, i want this no color
on the other contrary, row54, row59 & row60, this is not existed in workbook, so i want this black

2.jpg
 
Upvote 0
Hopefully, I have understood correctly.
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim v As Variant, desWS As Worksheet, i As Long
    Set desWS = Sheets("SUMMARY")
    With desWS
        v = .Range("A2", .Range("A" & .Rows.count).End(xlUp))
        For i = LBound(v) To UBound(v)
            If Evaluate("isref('" & CStr(v(i, 1)) & "'!A1)") Then
                If Sheets(CStr(v(i, 1))).Tab.Color <> 0 Then
                    .Range("A" & i + 1).Interior.Color = Sheets(CStr(v(i, 1))).Tab.Color
                End If
            Else
                .Range("A" & i + 1).Interior.Color = vbBlack
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hopefully, I have understood correctly.
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim v As Variant, desWS As Worksheet, i As Long
    Set desWS = Sheets("SUMMARY")
    With desWS
        v = .Range("A2", .Range("A" & .Rows.count).End(xlUp))
        For i = LBound(v) To UBound(v)
            If Evaluate("isref('" & CStr(v(i, 1)) & "'!A1)") Then
                If Sheets(CStr(v(i, 1))).Tab.Color <> 0 Then
                    .Range("A" & i + 1).Interior.Color = Sheets(CStr(v(i, 1))).Tab.Color
                End If
            Else
                .Range("A" & i + 1).Interior.Color = vbBlack
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
Hi mumps
thank you for your reply

it works wonderful

thank you very much
 
Upvote 0
My pleasure. :)
Hopefully, I have understood correctly.
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim v As Variant, desWS As Worksheet, i As Long
    Set desWS = Sheets("SUMMARY")
    With desWS
        v = .Range("A2", .Range("A" & .Rows.count).End(xlUp))
        For i = LBound(v) To UBound(v)
            If Evaluate("isref('" & CStr(v(i, 1)) & "'!A1)") Then
                If Sheets(CStr(v(i, 1))).Tab.Color <> 0 Then
                    .Range("A" & i + 1).Interior.Color = Sheets(CStr(v(i, 1))).Tab.Color
                End If
            Else
                .Range("A" & i + 1).Interior.Color = vbBlack
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
Hi mumps,

with more efficiency and convenience now, can i add a sorting function in this code, first when cell color index = 0, last when cell color index = black
ps: doesn't matter on other colors

thank you very much
 
Upvote 0

Forum statistics

Threads
1,216,272
Messages
6,129,822
Members
449,538
Latest member
cookie2956

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