[英]Can I remove a reference to a specific cell in a rule containing a formula so I can reuse it across multiple columns?
我正在尝试将相同的规则应用于不同的列,如果它是空的,它将用绿色填充单元格。
我通过录制宏获得了规则,但无法找到一种成功的方法来删除对下面范围 B2 的引用。
我想把它作为一项规则,我可以将其应用于多个选定的列。
有什么我可以代替的吗?
Public Sub FillGreenIfCellNotEmpty()
selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(B2))>0"
selection.FormatConditions(selection.FormatConditions.count).SetFirstPriority
With selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0
End With
selection.FormatConditions(1).StopIfTrue = False
End sub
我试过用“cells(1,1)”代替 B2 来引用选区的第一个单元格,并用“selection”代替。
目前,我不完全了解规则如何与即时更新一起使用。 我原以为公式会更符合 if not isempty(selection) 而不是 LEN() 和 TRIM()
请试试这个:
Option Explicit
Public Sub FillGreenIfCellNotEmpty()
Dim x As String
x = Selection.Address(0, 0)
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(" & x & " ))>0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
突出显示非空白单元格
Selection.FormatConditions.Add _
Type:=xlCellValue, _
Operator:=xlNotEqual, _
Formula1:="="""""
突出显示空白单元格
Selection.FormatConditions.Add _
Type:=xlCellValue, _
Operator:=xlEqual, _
Formula1:="="""""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.