VBA Code to set fill color of cells (criteria based)

Erudite.Warrior

New Member
Joined
Jun 13, 2006
Messages
7
Hello, first post. What an awesome resource! I had no idea there were so many other "Excel Geeks" like me in the world!

Here is my problem. I have exceeded the limits of conditional formatting. I want Excel to automatically set a fill color for cells based upon specific criteria. The criteria is the same that inputs the letters in the cells (T, A, B, C, D). I belive there is a way to do this using VBA, I just do not know how. Your help would be greatly appreciated.

The sheet below is a VERY SMALL example. In the project I am working on the month columns go out to 2020 and there are several more "Items" to be evaluated.

I have no experience wrting VBA. But have very dated experience writing code (15 years ago). The top spreadsheet is what I have now. The bottom is what I want it to look like (included for clarity).

Thanks in advance for your help!
Example Problem.xls
ABCDEFGHIJKLMN
1Person(T) 90-Days(A) 30-Days(B) 30-Days( C ) 60-Days(D) 30-DaysJan-06Feb-06Mar-06Apr-06May-06Jun-06Jul-06Aug-06
2John03-Jan-0601-May-0601-Apr-0617-Jul-0601-Dec-06TTTBA  C
3Pete16-May-0601-Sep-0601-Apr-0612-Feb-0601-Nov-06 CCB TT 
4Mike17-Jun-0601-Nov-0601-Feb-0601-Apr-0601-Jan-06DB CC TT
5
6
7Person(T) 90-Days(A) 30-Days(B) 30-Days( C ) 60-Days(D) 30-DaysJan-06Feb-06Mar-06Apr-06May-06Jun-06Jul-06Aug-06
8John03-Jan-0601-May-0601-Apr-0617-Jul-0601-Dec-06TTTBAC
9Pete16-May-0601-Sep-0601-Apr-0612-Feb-0601-Nov-06CCBTT
10Mike17-Jun-0601-Nov-0601-Feb-0601-Apr-0601-Jan-06DBCCTT
Example (3)
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Private Sub Worksheet_Change(ByVal Target As Range)
'Sheet module code like: Sheet1.

If Target.Value = "T" Then Target.Interior.ColorIndex = 35

If Target.Value = "" Then Target.Interior.ColorIndex = xlNone
End Sub


Just copy the "If" statement down as many times as you need for your conditions in this Event, then change the Trigger value [Case Sensitive!] and its resulting color!

To get the color index record a macro for the color you want.

This code is automatic.
 
Upvote 0
Thanks for the code. This gets me close but does not quite solve the problem. The code you provided applies the appropiate fill color ONLY when I type a "T", "A", "B", "C", or "D". It does not apply the formatting when the FORMULA RESULT is T, A, B, C, or D. The code should apply the fill color based upon the formula result AND apply the formatting retroactively. My error for not explicitly stating this.

As a side note. The code crashes when copying or deleting multiple cells that include T's, A's, B's, C's or D's. Also, the formatting is not applied retroactively (even when forcing a recalculation of the spreadsheet).

Any more ideas? Thanks!
 
Upvote 0
The Target in this case can only be one cell.

To get around this Use a loop that runs on the sheet's Change and Calculate Events!


Private Sub Worksheet_Calculate()
'Sheet module code, like: Sheet1.
myTest
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
'Sheet module code, like: Sheet1.
myTest
End Sub

Private Sub myTest()
'Sheet module code, like: Sheet1.

Dim myRng As Range

Set myRng = ActiveSheet.Range("A1: F33")

For Each Cell In myRng
If Cell.Value = "T" Then Cell.Interior.ColorIndex = 35

If Cell.Value = "" Then Cell.Interior.ColorIndex = xlNone
Next Cell
End Sub
 
Upvote 0
Joe Was,

Thank you SO MUCH!! You are definitely an Excel Jedi Master. I am humbled by your knowledge. I wish I could buy a beer (or three) to thank you!
 
Upvote 0

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