excel宏不能识别我的第二个IF条件

时间:2013-12-31 09:40:47

标签: excel vba excel-vba

我有以下代码来检查从E15开始的单元格与今天的日期相比是否具有超过45天的日期,这里的问题是它无法识别以下条件并且它将返回错误,下面是检查单元格的值是否为日期。如果有条件,下面有什么问题吗?

IsDate(.Range("E" & i)) = True





Sub Refees()
Dim ws As Worksheet
Dim lRow As Long, i As Long, OutputRow As Long
Dim copyRng As Range

'~~> Change this to the relevant worksheet
Set ws = ActiveWorkbook.Sheets("Warranty Quote 1 Year")

With ws
    '~~> Get LatRow in Col B
    lRow = .Range("B" & .Rows.Count).End(xlUp).Row
    OutputRow = lRow + 1

    '~~> Loop through the cells
    For i = 15 To lRow
        If DateDiff("d", .Range("E" & i).Value, Date) > 45 And IsDate(.Range("E" & i)) = True Then
            If copyRng Is Nothing Then
                Set copyRng = .Range("B" & i & ":I" & i)
            Else
                Set copyRng = Union(copyRng, .Range("B" & i & ":I" & i))
            End If
        End If
    Next i

    '~~> Copy the expired records in one go
    If Not copyRng Is Nothing Then
        copyRng.Copy .Range("B" & OutputRow)

        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        .Range("I" & OutputRow & ":I" & lRow).Value = "Reinstatement Fees"
    End If
End With
End Sub

1 个答案:

答案 0 :(得分:1)

尝试将您的代码更改为此(请先检查它是否为日期):

For i = 15 To lRow
   If IsDate(.Range("E" & i)) = True Then
        If DateDiff("d", .Range("E" & i).Value, Date) > 45 Then
            If copyRng Is Nothing Then
                Set copyRng = .Range("B" & i & ":I" & i)
            Else
                Set copyRng = Union(copyRng, .Range("B" & i & ":I" & i))
            End If
        End If
   End If 
Next i