Edd*_*die 2 excel vba conditional-formatting
我正在尝试编写一个子过程,它将一些条件格式应用于 Excel 中的一系列单元格。我有点卡住了,所以我使用了宏录制器。然而,我无法弄清楚为什么它应用下面的公式,并且当我手动运行代码时它失败了。
下面是录制的宏,它不起作用,而是将格式应用于错误的单元格。任何纠正它的帮助将不胜感激
谢谢
Sub MacroTest()
Range("Table1").Select
'The below formula is wrong but I can't figure out what it should be
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(TRIM(D15))=0"
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Run Code Online (Sandbox Code Playgroud)
小智 5
经过一番搜索后,我发现一个选项可以在不使用函数 LEN 的情况下工作,并且需要使用 xlBlanksCondition 指定范围。我不明白为什么宏记录器会提出 LEN 解决方案,如果它也可以使用 xlBlanksCondition 解决方案。
来源:MSDN 微软
我首先选择一个范围,然后应用以下代码:
With Selection.FormatConditions.Add(Type:=xlBlanksCondition)
.StopIfTrue = False
.Interior.PatternColorIndex = xlAutomatic
.Interior.Color = RGB(226, 80, 80)
.Interior.ThemeColor = xlThemeColorAccent2
.Interior.TintAndShade = 0.39
.Font.Color = vbBlack
.Font.TintAndShade = 0
.Borders.LineStyle = xlContinuous
.Borders.TintAndShade = 0
.Borders.Weight = xlThin
.Borders.Color = RGB(255, 0, 0)
.StopIfTrue = False
End With
Run Code Online (Sandbox Code Playgroud)