运行 VBA 中条件格式的时间错误

Run time error over conditional formatting in VBA

所以这是有效的,突出显示了 checkaddress 值右侧的单元格。现在不是,我不知道为什么。它给出了错误 "Run-time error '5': Invalid procedure call or argument"。错误在第一个.Add 语句中,但将其注释掉,所有.Add 语句错误。

Sub Highlight4()

For i = 1 To ActiveSheet.Cells(Rows.Count, 4).End(xlUp).row Step 2

    If Cells(i, 4) = "Metric" Then
        For j = 1 To 15

        Dim r As Range
        Set r = Range(Cells(i, j * 4 + 2), Cells(i + 1, j * 4 + 4))

        Dim checkAddress As String
        checkAddress = Cells(i, j * 4 + 1).Address

        With r.FormatConditions
            .Delete

            .Add Type:=xlExpression, Formula1:="=" & checkAddress & " = 0"
            .Item(.Count).Interior.Color = rgbRed

            .Add Type:=xlExpression, Formula1:="=" & checkAddress & " = 15"
            .Item(.Count).Interior.Color = rgbGold

            .Add Type:=xlExpression, Formula1:="=" & checkAddress & " = 25"
            .Item(.Count).Interior.Color = rgbGreen
        End With

        Next j
    End If
Next i
End Sub

你的代码没有问题。您收到该错误是因为 Excel 在 R1C1 模式下是 运行。关掉就好了:)

  1. 点击File | Options | Formulas
  2. 取消勾选R1C1 Reference Style