简体   繁体   English

条件格式化VBA不同单元格作为条件

[英]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) 条件应该进入此单元格: 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. 在Excel 2010中,不需要VBA。

  1. place Excel cursor in cell (y, cellcounter) and click [Conditional Formatting] - [New Rule...] from the Styles section of the Home ribbon 将Excel光标放在单元格(y,单元格计数器)中,然后单击“首页”功能区的“样式”部分中的[条件格式]-[新规则...]
  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) 单击单元格(x,单元格计数器)...,它将显示例如= $ A $ 1(即,满足您条件的单元格的引用)
  5. add ">0" to formula and select the format 在公式中添加“> 0”并选择格式

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. 您可以使用[条件格式设置]-[管理规则...]将相同的规则应用于多个单元格,因此,如果条件单元格从0变为1,则所有单元格都将变为红色。

To do the same in VBA, use the following methods and properties: 若要在VBA中执行相同的操作,请使用以下方法和属性:

{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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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