删除日期超过预定义日期的行



我想删除不在预定义范围(三月)之间的行。我认为问题出在某种程度上是数字格式。有什么建议吗?

frdatum = Format("01/03/2014", "dd/mm/yyyy")
tdatum = Format("30/03/2014", "dd/mm/yyyy")
valami = Format(frdatum, "dd/mm/yyyy")
valami2 = Format(tdatum, "dd/mm/yyyy")
Dim LValue As String

finalrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("C2:C" & finalrow).NumberFormat = "dd/mm/yyyy"

For i = finalrow To 2 Step -1
    If Not valami < Format(Cells(i, 3).Value, "dd/mm/yyyy") < valami2 Then
        Cells(i, 1).EntireRow.Delete
    End If
Next i

您试图比较字符串的值,就好像它们是数字考虑:

Sub qwerty()
    Dim frdatum As Date
    Dim tdatum As Date
    Dim LValue As String
    Dim d As Date
    frdatum = DateSerial(2015, 3, 1)
    tdatum = DateSerial(2015, 3, 31)
    finalrow = Cells(Rows.Count, 3).End(xlUp).Row
    Range("C2:C" & finalrow).NumberFormat = "dd/mm/yyyy"

    For i = finalrow To 2 Step -1
        d = Cells(i, 3).Value
        If d > tdatum Or d < frdatum Then
            Cells(i, 1).EntireRow.Delete
        End If
    Next i
End Sub

最新更新