简体   繁体   中英

Conditional formatting VBA different cell as condition

I would like to add conditional formatting to a cell, but the value for the condition is another cell.

The condition should go into this cell: Cells(x, cellcounter)

If Cells(x, cellcounter) > 0 then Cells(y, cellcounter) color RGB(153, 199, 112)

This is what I have come up with, but it gives me an error. What would be the correct syntax?

Cells(cell_quote_paid, cellcounter).FormatConditions.Add(Cells(cell_pending, cellcounter), xlGreater, "=0").Interior.Color = RGB(153, 199, 112)

Thx for the help!

In Excel 2010 there's no need for VBA.

  1. place Excel cursor in cell (y, cellcounter) and click [Conditional Formatting] - [New Rule...] from the Styles section of the Home ribbon
  2. click " Use a formula to determine which cells to format "
  3. place cursor in field "Format values where this formula is true"
  4. click in cell (x, cell counter) ... it will show eg =$A$1 (ie the reference of the cell delivering your condition)
  5. add ">0" to formula and select the format

You can apply the same rule to multiple cells using [Conditional Formatting] - [Manage Rules...], so all cells will turn red if condition cell goes from 0 to 1.

To do the same in VBA, use the following methods and properties:

{target range}.FormatConditions.Add Type:=xlExpression, Formula1:="=$A$1>0"
{target range}.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
{target range}.FormatConditions(1).Font.Color = RGB(153, 199, 112)
{target range}.FormatConditions(1).StopIfTrue = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM