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
 
This works for me:
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim v As Variant, desWS As Worksheet, i As Long, lRow As Long, dic As Object, k As Variant
    Set desWS = Sheets("SUMMARY")
    With desWS
        lRow = .Range("A" & .Rows.count).End(xlUp).Row
        v = .Range("A2:A" & lRow).Value
        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
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 1 To Sheets.count
        If Sheets(i).Name <> "PERSONALIZATION" And Sheets(i).Name <> "SUMMARY" Then
            If Sheets(i).Tab.Color <> False Then
                If Not dic.exists(Sheets(i).Tab.Color) Then
                    dic.Add Sheets(i).Tab.Color, Nothing
                End If
            End If
        End If
    Next i
    For Each k In dic.keys
        With Sheets("SUMMARY").Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("A2:A" & lRow), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal
            .SortFields.Add(Range("A2:A" & lRow), xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 0, 0)
            .SortFields.Add(Range("A2:A" & lRow), xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = k
            .SetRange Range("A1:Y" & lRow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next k
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
This works for me:
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim v As Variant, desWS As Worksheet, i As Long, lRow As Long, dic As Object, k As Variant
    Set desWS = Sheets("SUMMARY")
    With desWS
        lRow = .Range("A" & .Rows.count).End(xlUp).Row
        v = .Range("A2:A" & lRow).Value
        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
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 1 To Sheets.count
        If Sheets(i).Name <> "PERSONALIZATION" And Sheets(i).Name <> "SUMMARY" Then
            If Sheets(i).Tab.Color <> False Then
                If Not dic.exists(Sheets(i).Tab.Color) Then
                    dic.Add Sheets(i).Tab.Color, Nothing
                End If
            End If
        End If
    Next i
    For Each k In dic.keys
        With Sheets("SUMMARY").Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("A2:A" & lRow), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal
            .SortFields.Add(Range("A2:A" & lRow), xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 0, 0)
            .SortFields.Add(Range("A2:A" & lRow), xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = k
            .SetRange Range("A1:Y" & lRow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next k
    Application.ScreenUpdating = True
End Sub
Hi mumps
thank you for your reply

sorry for the late reply, as far as i tried this 2 weeks, it is just prefect!!

thank you very much for your help!:biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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