Autofilter date range not showing correct results

skull_eagle

Board Regular
Joined
Mar 25, 2011
Messages
89
I'm running the attached code, it is applying the correct filter on the correct column but the results displayed are incorrect.

When I manually apply the same filter, the correct results are displayed.

This is very strange and I don't understand why this is happening.
Any help would be greatly appreciated.
Thank you
VBA Code:
DateVal = Worksheets("Admin").Range("P6")

If ColNum2 = 99 Then
    
If Search_Form.Controls("CM_" & ColNum2) = "< 35" Then
  
        
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=6, _
    Criteria1:="<" & DateVal, Operator:=xlAnd
        
ElseIf Search_Form.Controls("CM_" & ColNum2) = "> 35" Then

    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=6, _
    Criteria1:=">" & DateVal, Operator:=xlAnd

ElseIf Search_Form.Controls("CM_" & ColNum2) = "CLEAR" Then

    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=6
    

        
End If
End If
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This is a bit of a guess, but I think when you concatenate the dateval with a string operator "<", the date becomes a string and loses its serial date value.

Try converting the dateval to a long-type numeric value then concatenating the operator.

Criteria1:="<" & Clng(DateVal)
 
Upvote 0
Solution
This is a bit of a guess, but I think when you concatenate the dateval with a string operator "<", the date becomes a string and loses its serial date value.

Try converting the dateval to a long-type numeric value then concatenating the operator.

Criteria1:="<" & Clng(DateVal)
Thank you so much!!!

It works perfectly.
 
Upvote 0
I had this same problem last week and it took a while for me to realise that dates are stored as a long type.

I'll never forget that.
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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