Conditional Formatting problem using VBA

musoguy

Board Regular
Joined
May 20, 2008
Messages
173
I am trying to write a piece of code that will apply conditional formatting to the ActiveCell:

The ActiveCell will always be in Column B, but the row number will change each time the macro is run.

The cell the conditional formatting refers to will always be in Column G.

However the row the cell is in should be the same row as the ActiveCell.

I can't work out how to write a code that will make the row number of the formula the same as the ActiveCell.

Below is what I have , but it doesn't work! Any help would be greatly appreciated.

Code:
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=Offset(0, 5) = 0"
Selection.FormatConditions(1).Interior.ColorIndex = 27
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi,

Not sure exactly about only wanting to apply to active cell. Can be to any range, BTW.

Suggest you use R1C1 style in conditional format formulas in VBA.

So, could be
Code:
   With ActiveCell
      .FormatConditions.Delete
      .FormatConditions.Add Type:=xlExpression, Formula1:="=RC7=0"
      .FormatConditions(1).Interior.ColorIndex = 27
   End With

To always refer to column G = column 7.

Or, to refer to the column 5 away, from B to G, it could be
Code:
   With ActiveCell
      .FormatConditions.Delete
      .FormatConditions.Add Type:=xlExpression, Formula1:="=RC[5]=0"
      .FormatConditions(1).Interior.ColorIndex = 27
   End With

HTH, Fazza
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

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