如果满足指定条件,我正尝试将不同颜色应用于B列和C列中的单元格。我有两个AND公式
我在函数中遇到无效的过程错误。 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
感谢您的帮助