Setting conditional formatting referencing a cell location with VBA

fredalina

New Member
Joined
Sep 14, 2011
Messages
46
Hi, all, new to this group!

I have a spreadsheet with dozens of worksheets. Each worksheet contains a series of data with two different totals columns (the data is arranged 2 ways per sheet). The two totals columns should equal if all works correctly in the data, which is linked via SumProduct. I want to do a little sanity check that will color the interior of one of the total columns bright red if the totals do not match, and I'd prefer to establish the conditional formats programmatically so as to avoid establishing them individually for each sheet.

The code to determine the location of the cell to apply the conditional formatting works perfectly, as does the code to determine which cell should be compared. However, at this point in time, when I click on the cell and click on Conditional Formatting, the formatting has a hard-coded value. Since the data will change monthly and I'd prefer to avoid running this macro each month, I'm wondering if it's possible for the conditional formatting to be established so that it references a cell location rather than its value?

Rich (BB code):
Set rg = Range(Cells(x, totalcol2).Address)
rg.FormatConditions.Delete
rg.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlNotEqual, Formula1:=totrg.Value                        rg.FormatConditions(1).Interior.ColorIndex = 3

works, but this sets the formatting to be the hard-coded value:
Cell Value is Not Equal To 2.1918

Rich (BB code):
Set rg = Range(Cells(x, totalcol2).Address)
rg.FormatConditions.Delete
rg.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlNotEqual, Formula1:=totrg.Address                        rg.FormatConditions(1).Interior.ColorIndex = 3

sets up the formatting to be:
Cell Value is Not Equal To ="$K$25"

In order for it to work as I hope it will, it needs to be:
Cell Value is Not Equal To =$K$25
(no quotation marks).

I tried using the Indirect() function, but apparently that doesn't work in VBA. Any suggestions?

I know it is possible to run the code each month as a subroutine when the data is loaded and reset the values in example 1 above, but the code to run the data is lengthy and I'd like to avoid additional steps.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
RESOLVED: Setting conditional formatting referencing a cell location with VBA

And... I've solved it.
Code:
Set rg = Range(Cells(x, totalcol2).Address)
rg.FormatConditions.Delete
rg.FormatConditions.Add Type:=xlCellValue, _
Operator:=xlNotEqual, Formula1:="=" & totrg.Address
rg.FormatConditions(1).Interior.ColorIndex = 3
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

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