Question

I know what I need to do, but I'm not really sure how to implement it through VBA.

Basically, I have a row of data that I need to format with a fill color.

A sample row of 20 data points may look something like this:

80 80 80 80 80 80 95 95 95 95 95 95 95 85 85 85 85 85 85 85

Here is my basic criteria... Anything equal to 90 or above will be GREEN at all times. Anything between 80 - 90 will be GREEN unless it is within 10 cell locations of the next reduction. If it is within 10 cell locations of the next reduction I need to to format YELLOW.

So in the above example, the first three 80s would be GREEN, the next three 80s would be YELLOW, the 95s would all be GREEN, and the 85s would all be GREEN.

I basically need the code to start in location X, read the next two locations (Y and Z), determine if Z is less than Y (if Z is not less than Y, Y and Z need to increment one cell each and then check if Z is less than Y again) AND Z is within 10 cell locations of X, and then format all cells from X until Y to be YELLOW.

The other (and maybe simpler) option would be to have the code look at the entire row of data, find all instances where Z is less than Y, then go back and determine if there are any cells within 10 cell locations that have values between 80-90 and color them YELLOW.

Can someone help me with this... I've learned through many hours of testing that it can't be done through traditional conditional formatting, and I don't have the VBA knowledge to come up with this code on my own.

Thanks for the help.

Was it helpful?

Solution

This is just a general answer to push you in the right direction.

Record a macro while doing the conditional formatting to get the basics:

    Range("H26").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=80", Formula2:="=90"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

Then use that to build some code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top