运行时错误5以编程方式应用条件格式时无效的过程调用或参数

时间:2015-12-28 16:06:40

标签: excel-vba vba excel

如果满足指定条件,我正尝试将不同颜色应用于B列和C列中的单元格。我有两个AND公式

  1. 如果B不是空白且G中的相应单元格不是空白。
  2. 如果C不为空且G中的相应单元格不为空白。
  3. 我在函数中遇到无效的过程错误。 r.FormatConditions.Add xlExpression, Formula1:=formula

    我做错了什么?

    请参阅以下代码:

        Public Sub applyColor()
    
        Dim ws As Worksheet, thisWs As Worksheet
    
        Dim myRangeB As Range
        Dim myRangeC As Range
        Dim lastRow As Long
    
        Set thisWs = ActiveSheet
        setSheets
    
    
        With ActiveSheet 'delete conditional formatting
            .Range("B:C").FormatConditions.Delete
            lastRow = .Range("A" & Rows.Count).End(xlUp).Row
            Set myRangeB = .Range("B2:B" & lastRow) 'The range to be formatted
            Set myRangeC = .Range("C2:C" & lastRow)
    
        End With
    
    
        For Each ws In ThisWorkbook.Worksheets
            If ws.name = wsColon.name Or ws.name = wsLung.name Or ws.name = wsMela.name Then
                ws.Activate
    
                Call FormatRange(myRangeB, 3, "=AND(NOT(ISBLANK($B1)),NOT(ISBLANK($G1)))")
                Call FormatRange(myRangeC, 29, "=AND(NOT(ISBLANK($C1)),NOT(ISBLANK($G1)))")
    
            End If
        Next
        thisWs.Activate
    
    End Sub
    
    Public Sub FormatRange(r As Range, colorIndex As Integer, formula As String)
    r.FormatConditions.Add xlExpression, Formula1:=formula
    r.FormatConditions(r.FormatConditions.Count).Interior.colorIndex = colorIndex
    End Sub
    

    感谢您的帮助

0 个答案:

没有答案