将两列与颜色进行比较

时间:2019-05-16 12:24:20

标签: excel vba

我有三列AA,AB和AE,我必须分别比较所有行的AA和AB列与AE列。宏应首先检查AA和AB列,并找出哪一列具有琥珀色,并且应将该列与相应的AE列进行比较(仅当该Amber列的值为“ High”时)才少于335天是否。

我尝试在下面的代码中仅比较两列AB和AE。如果AB列的值为“高”,它将检查相应的AE列的天数是否少于335天。

Dim High As Range
Dim StartDate As Date

StartDate = Date

   With ThisWorkbook.Worksheets("TEMPLATE")

LRow = .Cells(.Rows.Count, "AB").End(xlUp).Row
For Each High In .Range("AB11:AB" & LRow)
    If High.Value = "High" Then
        If IsDate(.Range("AE" & High.Row)) = False Then
            .Range("AE" & High.Row).Interior.Color = vbRed
        Else
            If DateValue(.Range("AE" & High.Row)) < StartDate - 335 Then .Range("AE" & High.Row).Interior.Color = vbRed
        End If
    End If
Next High

End With

1 个答案:

答案 0 :(得分:1)

我认为您正在尝试做的...如果没有,它至少应该使您知道从哪里开始:)

If(someValue = False)与else语句相反,将If翻转会很混乱

Dim High As Range
Dim StartDate As Date
Dim amberColor As Boolean

StartDate = Date

With ThisWorkbook.Worksheets("TEMPLATE")

    LRow = .Cells(.Rows.Count, "AB").End(xlUp).Row
    For Each High In .Range("AB11:AB" & LRow)
        ' Assume cells aren't amber
        amberColor = False
        ' If AA is Amber
        If .Range("AA" & High.Row).Interior.Color = RGB(255, 192, 0) Then
            'Move high to AA
            Set High = .Range("AA" & High.Row)
            'Color is amber
            amberColor = True
        ' If AB is Amber
        ElseIf .Range("AB" & High.Row).Interior.Color = RGB(255, 192, 0) Then
            ' High is already in AB, don't move
            ' Color is amber
            amberColor = True
        End If

        ' If the cell was amber and the value is High
        If amberColor And High.Value = "High" Then
            ' If AE contains a date
            If IsDate(.Range("AE" & High.Row)) Then
                ' If AE is more than 335 days ago
                If DateValue(.Range("AE" & High.Row)) < StartDate - 335 Then
                    ' Make AE red
                    .Range("AE" & High.Row).Interior.Color = vbRed
                End If
            Else
                ' AE isn't a date, make it red
                .Range("AE" & High.Row).Interior.Color = vbRed
            End If
        End If
    Next High

End With