vba 根据不同单元格中的值有条件地更改字体颜色

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

Conditional change of font color based on value in a different cell

excelexcel-vbacolorsexcel-formulaconditional-formattingvba

提问by Bruno Lopes

I want to change the color of the text inside a cell but in a way that is conditioned by an action in another cell, specifically insertion of X. For example:

我想更改单元格内文本的颜色,但方式受另一个单元格中的操作限制,特别是插入X. 例如:

BEFORE

Cell A1: without text  
Cell B1: text in red

ACTION: Xis inserted in cell A1

ACTION:X插入单元格A1

AFTER

Cell A1: `X`  
Cell B1: text in blue

Can this be done using conditional formatting or with a macro?

这可以使用条件格式或宏来完成吗?

采纳答案by pnuts

Please try a Conditional Formatting rule of:

请尝试以下条件格式规则:

=A1="X"  

with Applies to:

适用于:

=$B  

and blue font. (This will override a 'standard' formatting of red font for B1 when A1 is populated with X.)

和蓝色字体。(当 A1 被填充时,这将覆盖 B1 的红色字体的“标准”格式X。)

回答by Goos van den Bekerom

Yes it can.

是的,它可以。

If Range("A1") = "" Then
    Range("B1").Font.Color = vbRed
else
   Range("B1").Font.Color = vbBlue
End If

if its purely for the "x" change the 'else' line to this

如果纯粹是为了“x”,请将“else”行更改为此

ElseIf Range("A1") = "X"