VBA to filter (between two dates and times) based on one cell value

Durtug

New Member
Joined
Mar 18, 2014
Messages
14
Hi guys :)

I have a WorkSheet where I have the following info (A1 showing 29.09.2015 when formated as short date):
ABCDEFGH
1Value(Left(E2;5))WebIDIDAgentStartTimeOtherOther2Message
2TestTestTest29.09.2015 08:40TestTestSome text
3TestTestTest29.09.2015 15:40TestTestSome other text

<tbody>
</tbody>







The macro I am using, that not work properly is:

Sub Test()

Dim dDate As Date

Set rDate = Sheets("Test").Range("A1") 'Cell housing date & time



If Not IsDate(rDate) Then 'Check if valid

MsgBox "Wrong format in A1"

Exit Sub

End If



dDate = DateSerial(Year(rDate), Month(rDate), Day(rDate))



ActiveSheet.Range("$A$1:$I$100").AutoFilter Field:=5, Criteria1:= _
">=" & dDate & " 08:00:00", Operator:=xlAnd, Criteria2:="<=" & dDate & " 15:00:00"

End Sub




My problem is that this WorkBook will be updated on regular basis (and the date/time will be different each time).
I try to use a macro, and it do not give me an error, or in other ways look like it is not working.
But when I use it, all rows are filtered, and only the heading is showing.
When I the go manually to the filter option on E to look, it look like it are having a "Datefilter" activated.
I then go to the next level, and it's marked with "between", and push that to look.
A messages box is then opened, and showing the date/time I like to filter between.
Because it look right, I push "OK"

Suddenly the filter is working right, and show the rows I want :confused:

So the help I need is to understand why my macro seems tho work fine, but filter away all rows and not just the ones between my date/time.
And why it show the right filter options when I look manually, but first work properly when I hit "Ok"

Thanks to the community in advance :)
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,447
Latest member
M V Arun

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top