我正在为工作创建主计划,并且我正在尝试显示信息,因此对用户来说非常直观。
所以...我们希望能够在单元格K4中输入日期时突出显示单元格J4:L4。然后,我们想要将它循环到第4行到第2500行以及列M:0,P:R,S:U等......我写了下面的宏,但它只是突出显示第一行。有人能指出我正确的方向吗?
Sub Highlight()
'
' Highlight Macro
'
' Keyboard Shortcut: Ctrl+Shift+R
'
Dim kRange As Range, k As Integer, aaaFormat As FormatCondition
If ActiveSheet.Name <> Sheet1.Name Then Exit Sub
For k = 4 To 2500
Set kRange = Range("=$J4:$L4")
If kRange.FormatConditions.Count <> 0 Then
kRange.FormatConditions.Delete
Else
Set aaaFormat = kRange.FormatConditions.Add(xlExpression, xlFormula, "=$K4<>0")
aaaFormat.Interior.Color = 15773696
End If
Next k
End Sub
答案 0 :(得分:2)
当您循环循环时,您需要调整CF规则将控制的范围以及确定其结果的公式。
'this,
Set kRange = Range("$J4:$L4")
'becomes,
Set kRange = Range("$J"& k & ":$L"& k)
'and this,
Set aaaFormat = kRange.FormatConditions.Add(xlExpression, xlFormula, "=$K4<>0")
'becomes,
Set aaaFormat = kRange.FormatConditions.Add(xlExpression, xlFormula, "=$K" & k & "<>0")
但是,由于您已经使行相对且列绝对,您可以简单地将CF写入整个范围。
Sub Highlight()
' Highlight Macro
' Keyboard Shortcut: Ctrl+Shift+R
With Sheet1.Range("J4:L2500")
.FormatConditions.Delete
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=$K4<>0")
.Interior.Color = 15773696
End With
End With
End Sub