vba 根据单元格值格式化选定的列是奇数还是偶数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13681738/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:40:59  来源:igfitidea点击:

Formatting Selected Columns Depending Cell Value Is Odd Or Even

excelexcel-vbaconditional-formattingvba

提问by Ampi Severe

I have a sheet with several columns. Column A consists of numbers. Depending if the cell value in column A is even (or odd) I want to highlight several cells in that row, but not the entire row.

我有一张有几列的工作表。A 列由数字组成。根据 A 列中的单元格值是偶数(或奇数),我想突出显示该行中的几个单元格,而不是整行。

So far using conditionally formatting I have been able to add a formatting rule for each column I want to have highlighted. So for example if I want columns A, B and E highlighted I use three rules:

到目前为止,使用条件格式我已经能够为我想要突出显示的每一列添加格式规则。例如,如果我想突出显示 A、B 和 E 列,我会使用三个规则:

Formula: =ISEVEN(A1) - Highlight Format - Sheet!$A:$A100
Formula: =ISEVEN(A1) - Highlight Format - Sheet!$B:$B100
Formula: =ISEVEN(A1) - Highlight Format - Sheet!$E:$E100

...and etc for all additional columns.

...等所有附加列。

But this is very cumbersome, I don't know how to make a rule to cover it all in once or even if it is possible. I'm guessing VBA might be the solution, but I'm kinda a rookie there.

但这很麻烦,我不知道如何制定规则以一次性或什至可能涵盖所有内容。我猜 VBA 可能是解决方案,但我在那里有点菜鸟。

Any clues on how to attack this?

关于如何攻击这个的任何线索?

采纳答案by K_B

You should just fix your reference column in the formula for that (A becomes $A in this case). The ranges it applies for then can be all put in the same rule as well:

您应该只在公式中修复您的参考列(在这种情况下,A 变为 $A)。它适用的范围也可以放在相同的规则中:

Formula: =ISEVEN($A1) - Highlight Format - Sheet!$A:$B100;Sheet!$E:$E100

So no VBA required!

所以不需要VBA!

回答by bonCodigo

:) You do not need to sweat so much at all. I am not sure what your Highlight Formatfor. You can add the even, odd into the conditional formatting window/wizard text boxes and then choose the formatting colour accordingly.

:) 你根本不需要出那么多汗。我不确定你的目的是什么Highlight Format。您可以将偶数、奇数添加到条件格式窗口/向导文本框中,然后相应地选择格式颜色。

Try this out:

试试这个:

For even:

对于偶数:

=MOD(A1,2)=0

For odd:

对于奇数:

=MOD(A1,2)>0