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.
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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.