在 excel 用户表单中的两个日期之间排序

Sort between two dates in excel userform

我有一个包含多个日期的 excel 数据库。我的日期在 D 范围内,我想从我的范围中确定开始日期和结束日期。请帮忙!

我使用此代码:

ActiveSheet.ListObjects("Table_Name").Range.AutoFilter Field:=4, _
Criteria1:=txtSDate.Value, Operator:=xlAnd, Criteria2:=txtEDate.Value

我还有两个使用以下代码的按钮:

Private Sub txtEDate_Change()
If txtEDate.Text = "" Then
    MsgBox "You must enter a Date! "
    Exit Sub
End If
If txtEDate.TextLength = 2 Or txtEDate.TextLength = 5 Then
    txtEDate.Text = txtEDate.Text + "."
End If
If txtEDate.TextLength = 2 Or txtEDate.TextLength = 5 Then
txtEDate.Text = txtEDate.Text + "DD/MM/YYYY"
End If
End Sub

Private Sub txtSDate_Change()
If txtSDate.Text = "" Then
    MsgBox "You must enter a Date! "
    Exit Sub
End If
If txtSDate.TextLength = 2 Or txtSDate.TextLength = 5 Then
    txtSDate.Text = txtSDate.Text + "."
End If
If txtSDate.TextLength = 2 Or txtSDate.TextLength = 5 Then
txtSDate.Text = txtSDate.Text + "DD/MM/YYYY"
End If
End Sub

要获取开始和结束日期,只需使用

Sub test()
Dim Mindt As Date
Dim Maxdt As Date

Mindt = Application.WorksheetFunction.Min(Range("A1:A10")) 'change range as needed
Maxdt = Application.WorksheetFunction.Max(Range("A1:A10")) 'change range as needed

End Sub

范围内的单元格格式必须为日期