excel - 条件格式和 vba

标签 excel conditional-formatting vba

在宏中,我使用以下代码:

With Range("T2:X31")
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$R2=0"
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AE2"
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$AF2"
    .FormatConditions(1).Interior.Color = RGB(216, 216, 216)
    .FormatConditions(1).Font.Color = RGB(216, 216, 216)
    .FormatConditions(2).Interior.Color = RGB(255, 40, 40)
    .FormatConditions(2).Font.Color = RGB(255, 255, 255)
    .FormatConditions(3).Interior.Color = RGB(255, 128, 0)
    .FormatConditions(3).Font.Color = RGB(0, 0, 0)
End With

当我检查条件格式规则时,我想在 excel 中得到什么(手动输入):

enter image description here

我得到的是

enter image description here

除了行(1046053 而不是 2),一切都很好。我应该如何输入单元格引用以获得正确的公式。

最佳答案

在设置条件之前尝试选择A2:

Range("A2").Select

我从未使用过 VBA,但我怀疑其条件格式中的公式与当前选择有关。

我认为最好选择您实际设置条件的范围,就像您从界面中进行的那样:
Range("T2:X31").Select

关于excel - 条件格式和 vba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659075/

相关文章:

xml - XSD:向强类型 "simple"元素添加属性

python - 比较两个数据框中的多列并选择具有不同值的行

vba - 循环中的VBA错误处理

VBA - 如何匹配两个不同工作表中的标题以确保它们具有相同的名称和相同的顺序?

python - 从 Python 执行 VBAscript

vba - 仅适用于事件工作表,但我需要它在所有工作表上运行

excel - 对于循环仅循环一次并崩溃

vba - 用 Excel 和 VBA 在字符串中查找?

google-sheets - Google 工作表中已完成/到期任务的条件格式

excel - 更改excel单元格中文本的颜色