[英]How to Write VBA Conditional Formatting Between Cell Values (Invalid procedure, Call or argument)
I am trying to apply conditional formatting to cells when they are between certain values.当单元格介于某些值之间时,我正在尝试将条件格式应用于单元格。
I'm getting Run-time error '5': Invalid procedure call or argument.我收到运行时错误“5”:无效的过程调用或参数。
I need to use VBA to do this as the cell references (that I want to apply across) get messed up when I insert rows.我需要使用 VBA 来执行此操作,因为当我插入行时单元格引用(我想要应用的)会变得混乱。
I've looked at multiple pieces of sample code which approach the problem in different ways but when I try to replicate have no success.我查看了多段以不同方式解决问题的示例代码,但是当我尝试复制时没有成功。 I'm pretty new to VBA (and self taught) so this might be a very simple, obvious issue.
我对 VBA 很陌生(并且是自学的),所以这可能是一个非常简单、明显的问题。
With Sheets("Reconciliation").Range("K9:K66")
.FormatConditions.Delete
.FormatConditions.Add Type:=x1CellValue, Operator:=x1Between, Formula1:="=-10", Formula2:="=-0.1"
.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
.FormatConditions.Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
SOLUTION解决方案
When I use the macro recorder I get:当我使用宏记录器时,我得到:
Range("K9:K66").Select
ActiveWindow.SmallScroll Down:=-24
Range("K9:K66,O9:O66").Select
Range("O9").Activate
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=-10", Formula2:="=-0.1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
I modified this to be:我将其修改为:
Range("K9:K66,O9:O66").FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=-10", Formula2:="=-0.1"
Range("K9:K66,O9:O66").FormatConditions(Range("K9:K66,O9:O66").FormatConditions.Count).SetFirstPriority
With Range("K9:K66,O9:O66").FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Range("K9:K66,O9:O66").FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Range("K9:K66,O9:O66").FormatConditions(1).StopIfTrue = False
and it works now现在可以了
When I use the macro recorder I get:当我使用宏记录器时,我得到:
Range("K9:K66").Select
ActiveWindow.SmallScroll Down:=-24
Range("K9:K66,O9:O66").Select
Range("O9").Activate
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=-10", Formula2:="=-0.1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
I modified this to be:我将其修改为:
Range("K9:K66,O9:O66").FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=-10", Formula2:="=-0.1"
Range("K9:K66,O9:O66").FormatConditions(Range("K9:K66,O9:O66").FormatConditions.Count).SetFirstPriority
With Range("K9:K66,O9:O66").FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Range("K9:K66,O9:O66").FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Range("K9:K66,O9:O66").FormatConditions(1).StopIfTrue = False
and it works now现在可以了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.