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
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Sheet1 and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. The macro runs automatically whenever you activate Sheet1.
VBA Code:
Private Sub Worksheet_Activate()
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Range("A2:A21")
        If rng <> "" Then
            rng.Interior.Color = Sheets(rng.Value).Tab.Color
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Sheet1 and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. The macro runs automatically whenever you activate Sheet1.
VBA Code:
Private Sub Worksheet_Activate()
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Range("A2:A21")
        If rng <> "" Then
            rng.Interior.Color = Sheets(rng.Value).Tab.Color
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
Hi mumps
thank you very much for your reply

i use a test workbook to run my code to color tab first, and then do what you said
when i get back to sheet run, run time error 9 and yellow highlighted:
VBA Code:
rng.Interior.Color = Sheets(rng.Value).Tab.Color

beside, can i run my code and run this with a button?
thank you very much
 
Upvote 0
Delete the previous macro. Place this macro in a regular module and assign it to your button:
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Sheets("Sheet1").Range("A2:A21")
        If rng <> "" Then
            rng.Interior.Color = Sheets(rng.Value).Tab.Color
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Delete the previous macro. Place this macro in a regular module and assign it to your button:
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Sheets("Sheet1").Range("A2:A21")
        If rng <> "" Then
            rng.Interior.Color = Sheets(rng.Value).Tab.Color
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
Hi mumps
thank you very much for your reply

same result as yellow highlighted?
what did i wrong
VBA Code:
rng.Interior.Color = Sheets(rng.Value).Tab.Color

thank you very much
 
Upvote 0
I tested the macro on some dummy sheets and it worked properly. Could you upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here (de-sensitized if necessary).
 
Upvote 0
I tested the macro on some dummy sheets and it worked properly. Could you upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here (de-sensitized if necessary).
Hi mumps,
thank you very much for your reply

how about mycloud?
http://kel.ddns.net/f/80191ebf62/?raw=1
remarks:
i amend your code at module 4 with: range as A2:A1000, sheet2 named "summary" instead sheet1

if everything is fine,
row 749 should be colorindex 8
row 750 should be colorindex 7

thank you very much
 
Upvote 0
My anti virus software won't allow me to download the file using that link. Try DropBox.com or Box.com.
 
Upvote 0
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.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

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