簡體   English   中英

如何編寫 VBA 單元格值之間的條件格式(無效的過程、調用或參數)

[英]How to Write VBA Conditional Formatting Between Cell Values (Invalid procedure, Call or argument)

當單元格介於某些值之間時,我正在嘗試將條件格式應用於單元格。

我收到運行時錯誤“5”:無效的過程調用或參數。

我需要使用 VBA 來執行此操作,因為當我插入行時單元格引用(我想要應用的)會變得混亂。

我查看了多段以不同方式解決問題的示例代碼,但是當我嘗試復制時沒有成功。 我對 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

解決方案

當我使用宏記錄器時,我得到:

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

我將其修改為:

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

現在可以了

當我使用宏記錄器時,我得到:

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

我將其修改為:

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

現在可以了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM