need to change cell colour based on text

kit_kat328

New Member
Joined
May 17, 2011
Messages
7
Hi all,

I'm a complete newbie and when it comes to VBA, a pleb as well.

What I'm trying to do is get the cell to change background colour depending on what you choose from the drop down menu.

C2 - C300

drop down menu choice:

one = colour 53
two = colour 32
three = colour 42
four = colour 3
five = colour 43
six = colour 25
seven = colour 7
eight = colour 45

can anyone give me a hand? I've been trying to get an understanding of the basics for a couple days now, and just can't get a grasp on it:eeek:
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
  • What version of Excel do you have?
  • What cell(s), have the drop-down lists?
  • Do you want to change all cells in C2:C300 based on one drop-down list or do each of the cells in C2:C300 have the drop-down lists?
 
Upvote 0
  • What version of Excel do you have?
  • What cell(s), have the drop-down lists?
  • Do you want to change all cells in C2:C300 based on one drop-down list or do each of the cells in C2:C300 have the drop-down lists?

I'm using excel 2007, but I need this to work on 2002. Each cell from C2-C300 have a drop down menu each and I want the cell to change colour based on what you choose from the drop down menu.
 
Upvote 0
  • Right-clcick on the sheet tab that has the drop-down lists
  • Select from the pop-up menu View Code
  • Paste the code below in the VBA edit window.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim cell As Range
    
    If Not Intersect(Range("C2:C300"), Target) Is Nothing Then
    
        Application.EnableEvents = False
        For Each cell In Intersect(Range("C2:C300"), Target)
            With cell.Interior
            Select Case LCase(cell.Value)
                Case "one": .ColorIndex = 53
                Case "two": .ColorIndex = 32
                Case "three": .ColorIndex = 42
                Case "four": .ColorIndex = 3
                Case "five": .ColorIndex = 43
                Case "six": .ColorIndex = 25
                Case "seven": .ColorIndex = 7
                Case "eight": .ColorIndex = 45
                Case Else: .ColorIndex = xlNone
            End Select
            End With
        Next cell
        Application.EnableEvents = True
        
    End If
    
End Sub
 
Upvote 0
do i do anything with the 2 drop down menus at the top of the vba edit menu?
they are currently set to Worksheet and Change
 
Upvote 0
do i do anything with the 2 drop down menus at the top of the vba edit menu?
they are currently set to Worksheet and Change

The two VBA drop-downs should say Worksheet and Change

I'm guessing you may have previously disabled the Application.Events when you were writing your own VBA code. If yes, you need to re-enable the events so that this macro automatically triggers when you make a change to the worksheet.

Paste this macro in a standard module (not in the ThisWorkbook module) and run it.
Code:
Sub ReEnable_Events()
Application.EnableEvents = True
MsgBox "Events are enabled."
End Sub

You only need to run this macro this one time to reset your current session in Excel.
 
Upvote 0
no...it was me being a clumsy fool and put a decimal in the code by accident. I really do appreciate your help with this. You must get tons of muppets like me in here...lol
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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