Fenrirshowl
Technical User
Hi guys
I have data on a sheet called INPUT, which is effectively a record of post received - the date received is in column B / second column.
I want to take a copy of filtered information and put it on another sheet for amendment. After amendment changes are posted back to the INPUT sheet.
One of the filters that can be applied relates to date ranges on the "Date received" field.
The parameters are entered via a form. The FROM date is read from a textbox called tbDTRec and TO date is from a text box called tbDtRec2. The FROM date is always non-empty.
cbFrom is a combobox which has the entries "On" and "From" to define whether there is a "To" date.
Everything works fine, except when I am trying to filter between two dates when it returns no records, and I have no clue why. (I can exclude all other parameters of the filter and still get no records when filtering between two dates - I therefore think it has to be something about the piece of code below).
Can anyone explain what I have done wrong and/or provide a fix please?
Thanks in advance
Fen
I have data on a sheet called INPUT, which is effectively a record of post received - the date received is in column B / second column.
I want to take a copy of filtered information and put it on another sheet for amendment. After amendment changes are posted back to the INPUT sheet.
One of the filters that can be applied relates to date ranges on the "Date received" field.
The parameters are entered via a form. The FROM date is read from a textbox called tbDTRec and TO date is from a text box called tbDtRec2. The FROM date is always non-empty.
cbFrom is a combobox which has the entries "On" and "From" to define whether there is a "To" date.
Everything works fine, except when I am trying to filter between two dates when it returns no records, and I have no clue why. (I can exclude all other parameters of the filter and still get no records when filtering between two dates - I therefore think it has to be something about the piece of code below).
Can anyone explain what I have done wrong and/or provide a fix please?
Thanks in advance
Fen
Code:
dt1 = DateSerial(Year(tbDtRec.Text), Month(tbDtRec.Text), Day(tbDtRec.Text)) 'just to ensure we are dealing with a date!
If tbDtRec2.Text <> "" Then dt2 = DateSerial(Year(tbDtRec2.Text), Month(tbDtRec2.Text), Day(tbDtRec2.Text)) Else dt2 = ""
If cbFrom.Text = "On" Then
Selection.AutoFilter Field:=2, Criteria1:=dt1
ElseIf cbFrom.Text = "From" And tbDtRec2.Text = "" Then
Selection.AutoFilter Field:=2, Criteria1:=">=" & dt1
Else
Selection.AutoFilter Field:=2, Criteria1:=">=" & dt1, Operator:=xlAnd _
, Criteria2:="<=" & dt2
End If