大于或等于日期的条件

时间:2017-03-31 04:26:11

标签: excel vba excel-vba

为什么大于或等于条件不起作用。

Dim start As String
Dim endat As String
start = InputBox("Enter the Starting Date:        Format:  dd-mon-yy")
endat = InputBox("Enter the End Date:        Format:  dd-mon-yy")
Sheets("Data Base").Range("A1:H2000").AutoFilter Field:=5, Criteria1:=">=" & start, Operator:=xlAnd, Criteria2:="<=" & endat

1 个答案:

答案 0 :(得分:0)

如果“数据库”表格中E列中的值格式为Date,则可以强制InputBox仅允许日期格式。

使用Application.InputBoxFormatDateTime功能会强制用户以日期格式输入。

详细了解FormatDateTime MSDN

<强>代码

Option Explicit

Sub BetweenDates()

Dim StartDate   As Date
Dim EndDate     As Date

Do
    StartDate = Application.InputBox("Enter the Starting Date:", "Format: dd-mm-yyyy", FormatDateTime(Date, vbShortDate), Type:=1)
Loop While StartDate = False ' disable the Cancel option

Do
    EndDate = Application.InputBox("Enter the End Date:", "Format: dd-mm-yyyy", FormatDateTime(Date, vbShortDate), Type:=1)
Loop While EndDate = False ' disable the Cancel option

Sheets("Data Base").Range("A1:H2000").AutoFilter Field:=5, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate

End Sub