在excel userform中的两个日期之间排序

时间:2015-02-18 08:30:45

标签: excel vba 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

1 个答案:

答案 0 :(得分:0)

要获得开始和结束日期,请使用

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

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