0

从 datepicker 传递日期以对 excel 表进行排序时,格式在某处变得混乱并 dd/mm/yyyy mm/dd/yyyy

我检查了我的区域设置,它们设置为dd/mm/yyy

Sub sortFuel()

With UserForm1
    With .dp_StartDate
        startDate = Format(.Value, "dd/mm/yyyy")
    End With
    With .dp_EndDate
        endDate = Format(.Value, "dd/mm/yyyy")
    End With
End With

MsgBox startDate
MsgBox endDate

With ws
    .ListObjects("Table_DFDBMain_FuelTrans4").Range.AutoFilter Field:= _
    3, Criteria1:=">=" & Format(startDate, "dd/mm/yyyy"), Operator:=xlAnd, Criteria2:="<=" & Format(endDate, "dd/mm/yyyy")
End With

End Sub

我表中的列已经按 dd/mm/yyyy 排序,但是如果我选择 2012 年 5 月 7 日,它会在 2012 年 7 月 5 日出现。注意:它只会在日列小于 12 时翻转。例如,如果我选择6 月 24 日为开始日期,它将保持 6 月 24 日。

任何帮助将不胜感激!

4

1 回答 1

0

试试这个

Sub sortFuel()
    Dim D1 As Long, D2 As Long

    With UserForm1
        With .dp_StartDate
            startDate = Format(.Value, "dd/mm/yyyy")
        End With
        With .dp_EndDate
            endDate = Format(.Value, "dd/mm/yyyy")
        End With
    End With

    D1 = Val(Format(startDate, "0"))
    D2 = Val(Format(endDate, "0"))

    With ws
        .ListObjects("Table_DFDBMain_FuelTrans4").Range.AutoFilter Field:= _
        3, Criteria1:=">=" & D1, Operator:=xlAnd, Criteria2:="<=" & D2
    End With
End Sub
于 2012-06-12T06:40:13.283 回答