在我的Excel工作表中,第一个条件是基于行和列的文本匹配,以BLUE突出显示相交的单元格。
第二个条件:如果单元格值(日期格式)小于今天的日期,则以蓝色突出显示的单元格值必须更改为红色。
我能够满足第一个条件,但不能满足第二个条件。
Excel数据如下所示:
第一个条件:
第二个条件:问题我正面临着红色的内饰
我正在尝试使用VBA代码,如下所示:
Sub RunCompare()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim cols As Range, rws As Range
Dim lastRow As Integer: lastRow = ws.UsedRange.Rows.Count
Dim lastColumn As Integer: lastColumn = ws.UsedRange.Columns.Count
For Each cols In ws.Range(ws.Cells(4, 1), ws.Cells(4, lastColumn))
If cols.Value <> vbNullString Then
For Each rws In ws.Range("A1:A" & lastRow)
'first condition statement
If (rws.Value = cols.Value) Then
ws.Cells(rws.Row, cols.Column).Interior.Color = RGB(15, 219, 241)
End If
'second condition statement
If (rws.Value = cols.Value) < Date Then
ws.Cells(rws.Row, cols.Column).Interior.Color = RGB(255, 0, 0)
End If
Next
End If
Next
End Sub
使用条件格式可以很容易地做到这一点。
根据以下公式添加两个规则:
红色:=AND($A3=B$1,B3<>"",B3<TODAY())
。
蓝色: =AND($A3=B$1,B3<>"")
如果您确实想保留当前的VBA,则可以进行更改
If (rws.Value = cols.Value) < Date Then
至
If (rws.Value = cols.Value) And (ws.Cells(rws.Row, cols.Column).Value < Date) Then
或者,您可以通过将RED条件移动到现有BLUE条件检查中来进一步简化操作(rws.Value = cols.Value
红色和蓝色都必须为true)。
If rws.Value = cols.Value Then
With ws.Cells(rws.Row, cols.Column)
If .Value < Date Then
.Interior.Color = RGB(255, 0, 0) ' RED
Else
.Interior.Color = RGB(15, 219, 241) ' BLUE
End If
End With
End If
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句