VBA - Highlight cell if greater or less than in dynamic range

Maleko-1

New Member
Joined
Oct 20, 2010
Messages
27
Hi there,
I can't find an answer to this in VBA.

Cell D5 has a value (upper limit) and cell D6 has a value (lower limit).
From D10 down to the end of data in column D are values that if falls outside of the upper and lower limit above I need the cell highlighted red.
Another catch is...I need the same scenario to happen in all other columns to the right that has data.
For example, E5 and E6 (upper and lower limits) and E10 down has the data and so on to the last column that has data.

If anyone can help me with this it would be so appreciated and I would forever be in debt to you. :)

Thank you
Marc
 
if the range was D10:X33 it would look like

Code:
Sub Macro1()
    Range("D10:X33").FormatConditions.Add Type:=xlExpression, Formula1:="=OR(D10<D$6,D10> D$5)"
    Range("D10:X33").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Range("D10:X33").FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
End Sub

just change the range to suit you
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
for some reason the less than/greater than symbols won't work on this sheet. Formula1:= should equal the below

"=OR(D10 less than symbol D$6,D10 greater than symbol D$5)"
 
Upvote 0
The range will always be changing.
Also, is that formula referring to D5 and D6 for the entire range? I need to refer to rows 5 and 6 in each column as these values will be different for the data in each column from row 10 down.
 
Upvote 0
yes but that's why they have the $$ signs so as the spread across they will change to E5/E^ etc but always on the 5/6 row. all you need to change is the range. I have no other information to go with
 
Upvote 0
Thanks BarryL for your help. I will try to work with this to see if I can get the results I need.

I appreciate your time and help with this.
 
Upvote 0
Okay, thanks to your help it is doing what I wanted.
The only issue is that I have to make the range an exaggerated size so that it will cover any possible number of rows and columns. This leaves a lot of highlighted empty rows beneath the last row of data.

Is there a way to use your code but make it stop highlighting after the last row with data?

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,953
Members
449,198
Latest member
MhammadishaqKhan

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