VB Code to change cell fill colour based on searching for key word

Twin

New Member
Joined
Aug 27, 2011
Messages
6
I am trying to write a macro that I can run in different worksheets that will search the work sheet for key words and when it finds them changes the fill colour of the cell that the words are in.

Very High: dark Red
High: Red
Medium: Orange
Low: green
Very Low:light green

I am using excel 03 so conditional formatting isn't enough.

Is this possible?

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Paste the below code into your THISWORKBOOK Module: Reassign the color code numbers
currently assigned below (Lines 2 thru 6) before continuing...

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
VH = 3
H = 4
M = 5
L = 6
VL = 7
Set Rng = ActiveSheet.UsedRange
     For Each C In Rng
        Select Case C.Value
        Case vbNullString
            C.Interior.ColorIndex = xlNone
        Case "Very High"
            C.Interior.Color = VH
        Case "High"
            C.Interior.ColorIndex = H
        Case "Medium"
            C.Interior.ColorIndex = M
        Case "Low"
            C.Interior.ColorIndex = L
        Case "Very Low"
            C.Interior.ColorIndex = VL
        Case Else
            C.Interior.ColorIndex = xlNone
        End Select
    Next C
End Sub
 
Upvote 0
correction...

Case "Very High"
C.Interior.Color = VH

should be

Case "Very High"
C.Interior.ColorIndex = VH
 
Upvote 0
Thanks for the code. It looks like what i am after. Although i am having a problem copying it into a module.

I copy it into a module but I am struggling to get it to runs as when I press the 'play' button it asks for a macro name?

I am quite new to macros and VB so i think i am missing something really simple..

Any advice would be appreicated

Thanks
 
Upvote 0
The Revised code goes into your THISWORKBOOK module of the VBE.
From your Spreadsheet, press ALT+F11 (This will OPEN the VBE)
In the left-hand Pane look for THISWORKBOOK << Double Click on it !!
A code window should Open to the RIGHT.

Paste the revised code into this Window.

The Macro RUNS AUTOMATICALLY, in ALL shetts of your WB when you make
a change to any cells.

HTH,

Jim
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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