Help with vb and conditional formatting
I tested this code and it works. Paste the code in a standard module
and test it out first by stepping through the code one line at a time.
Make sure you change "mySheet" to the name of the actual worksheet you
are using this code on. I recommend you use a copy of your data to
test it first.
Option Explicit
Sub TotalCheck()
Dim rng As Excel.Range
Dim cell As Excel.Range
Dim ColBVal As String
Dim ColCVal As String
Set rng = Worksheets("mySheet").Range("A1", _
Worksheets("mySheet").Range("A65536").End(xlUp))
For Each cell In rng.SpecialCells _
(xlCellTypeConstants, 2) 'text values
ColBVal = cell.Offset(0, 1).Value
ColCVal = cell.Offset(0, 2).Value
If UCase$(cell.Value) = "TOTAL" Then
'check col B
Select Case CDbl(ColBVal)
Case Is < 0.9
'green
cell.Offset(0, 1).Interior.ColorIndex = 4
Case Is 1.1
'yellow
cell.Offset(0, 1).Interior.ColorIndex = 6
End Select
'check col C
Select Case CDbl(ColCVal)
Case Is < 0.9
'green
cell.Offset(0, 2).Interior.ColorIndex = 4
Case Is 1.1
'yellow
cell.Offset(0, 2).Interior.ColorIndex = 6
End Select
End If
Next cell
End Sub
On Apr 4, 8:39*am, CB wrote:
JP,
Thanks so much for your time and help.
Let me see if I can be more specific. *The spreadsheet has subtotals. *
Hence, I want to color code the totals rows for Col B and C if they meet the
criteria, if not no color.
Given the example below,
Row one would be skipped because A1 does not contain the word "Total".
A2 contains the word "Total", *Therefore B2 is less than 90% and would color
fill with green and still display the text 50%. *C2 is also less than 90 %
and would color fill with green and still display the text 75%.
If B2 or C2 were greater than 110% then they would color fill with yellow
displaying the text in their respective cells.
If B2 or C2 contained data between 90% and 110%, then no action would be
taken.
Thanks again for your help.
Cb
|