Question

When passing a date from datepicker to sort an excel table, somewhere the format is getting confused and swapping from dd/mm/yyyy to mm/dd/yyyy

I have checked my regional settings and they are set to 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

The columns in my table are already sorted in dd/mm/yyyy, yet if i select 7th May 2012, it comes out 5th July, 2012. NOTE: it only flips if the day column is less than 12. If for example i select 24th June for start date, it will stay 24th June.

Any help will be greatly appreciated!

Was it helpful?

Solution

Try this

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top