VBA Code For Changing Tab Color Based On Cell Values In Drop Down List

systemsrool

New Member
Joined
Nov 5, 2015
Messages
14
I am trying to change the color of a tab based on the cell value in B5. B5 is a drop down list using data validation that contains four entries: "Flow Rack" , "Workstation" , "Machine Guarding" , and "Other (custom)". A tab is named after each one of these as well. If someone selects "Workstation" from the drop down menu then I would like the "Workstation" tab to turn green. If any of the other three options are selected I want the "Workstation" tab to be red.

Any help is much appreciated. I only have very little experience with coding.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this:
To install this code:

Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window




Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B5")) Is Nothing Then
If Cells(5, 2).Value = "Workstation" Then
Sheets("Workstation").Tab.ColorIndex = 4
Else
Sheets("Workstation").Tab.ColorIndex = 3
End If
End If
End Sub
 
Upvote 0
Did you insert this script in your sheet? It does not get put in a module.
Are you sure the spelling is the same. You said "Workstation" not "WorkStation" and not "Work Station"
 
Upvote 0
I did the steps that you listed above the code. Right the sheet, view code, copy/paste, and then saved that. Am I missing something? and everything is spelled exactly the same.
 
Upvote 0
Have you run macros on your sheet before?
Does your workbook name end with .xlsm.
And is your Workbook allowed to run Macros?
And the value in cell B5 is "Workstation"
 
Last edited:
Upvote 0
Yes and it saved with .xlsm.
Yes. It's not saving as a macro from what I can tell.
I debugged and clicked run and it comes up with a prompt about naming the macro.
 
Upvote 0
What do you mean you clicked run.
This is not a Macro that you run from the macro recorder.
This macro runs when you change the value in cell B5.
That's why I said this macro does not go into a module.
If you right clicked the sheet tab and then choose view code and then choose insert and put this script into a module it will not run.
You must right click the Workstation tab choose view code and paste it into the window you see. Not in a module. And then close the window change the value in B5 and your tab color will change
 
Last edited:
Upvote 0
I did just as you said in the last line and it's not doing anything.
Does this work if B5 is a dropdown list using data validation?
 
Upvote 0
I got it. I was right clicking the "Workstation" tab and doing all of this.. didn't know I was supposed to put in the code for the first tab.
 
Upvote 0

Forum statistics

Threads
1,214,613
Messages
6,120,515
Members
448,968
Latest member
Ajax40

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