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

时间:2015-03-05 22:00:56

标签: excel-vba vba excel

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

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

1 个答案:

答案 0 :(得分:1)

您正在尝试比较字符串的值,就好像它们是数字一样考虑:

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