vba 给一个单元格分配背景颜色,给定另一个单元格的条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11977095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning a background color to a cell, given a condition on another cell
提问by JSW189
I am trying to create a formula in Microsoft Excel 2010 to accomplish the following algorithm:
我正在尝试在 Microsoft Excel 2010 中创建一个公式来完成以下算法:
If A1=10, then A2 has a background color of red.
如果 A1=10,则 A2 的背景颜色为红色。
I have basic knowledge of if/then statements in Excel, so I have this so far:
我对 Excel 中的 if/then 语句有基本的了解,所以到目前为止我有这个:
=IF(A1=10, x)
...where x
would be the formula for changing the background color of the cell. However, after conducting some internet research on the subject, the consensus seems to be that I should instead use conditional formatting to accomplish this task. I have never used conditional formatting and am having trouble understanding how to utilize it so that a condition on one cell would affect another cell.
...x
更改单元格背景颜色的公式在哪里。然而,在对该主题进行了一些互联网研究之后,共识似乎是我应该使用条件格式来完成这项任务。我从未使用过条件格式,并且无法理解如何使用它,以便一个单元格上的条件会影响另一个单元格。
This is the conditional formatting screen that I am faced with:
这是我面临的条件格式屏幕:
So I guess I have two questions:
所以我想我有两个问题:
- Is using conditional formatting the best way to accomplish this?
- If so, how can I utilize it to assign a background color to a cell, given the truth of a condition on a different cell?
- 使用条件格式是实现此目的的最佳方法吗?
- 如果是这样,考虑到不同单元格上的条件的真实性,我如何利用它为单元格分配背景颜色?
回答by RocketDonkey
You can do it using conditional formatting, so you're on the right track. One thing I'm assuming here is that you want to format the cell to the right of the cell with the value - is that correct? If so, you actually don't need to use an if
formula (though your logic is correct). In that dialog box, make the formula:
您可以使用条件格式来完成此操作,因此您走在正确的轨道上。我在这里假设的一件事是,您想使用该值格式化单元格右侧的单元格 - 对吗?如果是这样,您实际上不需要使用if
公式(尽管您的逻辑是正确的)。在该对话框中,创建公式:
=A1=10
And then when you click OK, change the Applies torange by clicking on the little chart icon next to it and clicking cell B1. Then do apply, etc., close out the box and try typing 10 in A1
. When you need to expand the range, you can just change that Applies torange to encompass everything you want affected. In the example below, cells B1:B26
will all change if 10 is entered in the cell directly to the left (column A).
然后当您单击确定时,通过单击旁边的小图表图标并单击单元格B1来更改适用于范围。然后执行应用等操作,关闭框并尝试在. 当您需要扩展范围时,您只需更改适用于范围以涵盖您希望影响的所有内容。在下面的示例中,如果直接在左侧的单元格(A 列)中输入 10 ,单元格将全部更改。A1
B1:B26