Excel 2013 使用宏比较不同行的日期 "Run time error 13, Type mismatch"

Excel 2013 compare dates on different row using macro "Run time error 13, Type mismatch"

我是 vba 的新人,目前我正在工作,我的老板需要我创建一个 excel 宏。

宏函数有:

所以现在我一直坚持到 "Run time error 13, Type mismatch"。

以下是我的编码部分,不匹配是在>>> <<<之间。

Private Sub Worksheet_Activate()
Dim rngA As Range
Dim rngD As Range

With Worksheets("Sheet1")

    For Each rngA In .Range(.Range("A1"), .Cells(.Rows.Count, "A").End(xlUp))
    For Each rngD In .Range(.Range("D1"), .Cells(.Rows.Count, "D").End(xlUp))

        >>> If rngA.Value("A1:xlUp") >= rngD.Value("D1:xlUp") Then <<<
            MsgBox "Sheet 1 Row " & rngA.Row & " expiring"
            rngC.Interior.ColorIndex = 3
            rngC.Font.ColorIndex = 2
            rngC.Font.Bold = True

        End If


    Next rngD
    Next rngA


End With
End Sub

请试试这个

With Worksheets("Sheet2")
lastrow = Range("A1048576").End(xlUp).Row
For i = 2 To lastrow
    If Range("A" & i).Value <> "" And Range("D" & i).Value <> "" Then
        If Range("A" & i).Value >= Range("D" & i).Value Then
            Range("C" & i).Value = "Contract Going to Expire"
            Range("C" & i).Interior.Color = 3
            Range("C" & i).Font.ColorIndex = 2
            Range("C" & i).Font.Bold = True
        End If
    End If
Next i

结束于