Formatting Multiple Rows

kipper19

Board Regular
Joined
Apr 12, 2014
Messages
92
Office Version
  1. 365
Platform
  1. Windows
Looking for some help.
I have the following formula which highlights a cell ok if the number in cell A1 is greater than B1 but I'm not sure how to expand the formula for multiple rows, I have around 200 rows of data that I would like to highlight when the number in the A column is greater than its corresponding number in the B Column etc

Sub highlight()
Dim rg As Range
Dim cond1 As FormatCondition
Set rg = Range("A1", Range("A1").End(xlDown))
rg.FormatConditions.Delete
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=$B$1")
With cond1
.Interior.Color = RGB(204, 255, 255)
End With
End Sub
 

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.
Note that your code is checking for equal to B1, not greater than:
Excel Formula:
"=$B$1"

To get it so that each cell looks at the value in column A in the same row, remove the absolute reference in front of row 1 that locks the row at 1, i.e.
Excel Formula:
"=$B1"

This will allow the row number to "float" as you go down rows, while locking the column reference in at column B.

See here for a good explanation on the different type of range references (Absolute, Relative, and Mixed):
 
Upvote 0
Solution
Change the formula in FormatConditions from Absolute to Mixed with Relative on the number of row.
VBA Code:
from:
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=$B$1")
to:
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=$B1")
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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