运行时错误在VBA条件格式

所以这是工作,突出checkaddress值右侧的单元格。 现在不是,我不知道为什么。 它给出错误“运行时错误”5:无效的过程调用或参数“。 错误是在第一个.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 File | Options | Formulas
  2. 取消选中R1C1 Reference Style

在这里输入图像说明