Autofilter Issue

skull_eagle

Board Regular
Joined
Mar 25, 2011
Messages
89
Hi,

I'm trying to filter a column by date.

Less than cell value to greater than cell value. Cell value is in Date format.

Instead of using the date in the filter it is using the numeric value of the date eg. 32300

I don't understand why the below won't appear as 06/06/1988 instead of 32300
Criteria1:="<" & Format([DateVal], "dd/mm/yyyy")

Any help would be greatly appreciated.


VBA Code:
Public WithEvents mLabelGroup2 As MSForms.ComboBox

Private Sub mLabelGroup2_Click()

''-- A live filter based on the value of the Combo boxes on the search form

Dim ControlName2, ColNum2
Dim Rowz As Long
Dim wsRD As Worksheet
Dim DateVal As Date

On Error Resume Next

    Set wsRD = Worksheets("RawData")
    Set wsRD = ActiveSheet


ControlName2 = mLabelGroup2.Name

ColNum2 = Replace(ControlName2, "CM_", "")


i = Search_Form.Controls("CM_" & ColNum2).Value


If Search_Form.Controls("CM_" & ColNum2).Value = "" Then
   
        
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum2
    
Else
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=ColNum2, _
    Criteria1:=i
                         
End If

           
DateVal = Worksheets("Admin").Range("P6")

If ColNum2 = 6 Then
    
If Search_Form.Controls("CM_" & ColNum2) = "< 35" Then
    
    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=6, _
    Criteria1:="<" & Format([DateVal], "dd/mm/yyyy")
    
    
ElseIf Search_Form.Controls("CM_" & ColNum2) = "> 35" Then

    ActiveSheet.ListObjects("Table_RawData").Range.AutoFilter Field:=6, _
    Criteria1:=">" & Format([DateVal], "dd/mm/yyyy")

Else

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

End If
End If
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If you are wanting to to filter dates using greater than you want it to filter based the numeric value, not a text string which is what you are doing using Format
 
Upvote 0
If you are wanting to to filter dates using greater than you want it to filter based the numeric value, not a text string which is what you are doing using Format

But if all the Values in the column are in Date format the numeric value doesn't work.
I've just recently added the formatting code to try and get it working.
 
Upvote 0
A format is just an appearance of the cell, you want to use it's numeric value.
For example with the data set below (with the dates formatted as dd/mm/yyyy)
Book1.xlsb
CDE
115/02/2023
2test
310/02/2023
409/02/2023
514/02/2023
620/02/2023
719/02/2023
813/02/2023
926/02/2023
1023/02/2023
1117/02/2023
1212/02/2023
1318/02/2023
1425/02/2023
1522/02/2023
1607/02/2023
1724/02/2023
1821/02/2023
1905/02/2023
2006/02/2023
2116/02/2023
2208/02/2023
2311/02/2023
Sheet1

using the code
VBA Code:
Sub Filterit()
  
   Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row).AutoFilter 1, ">" & CLng(Range("E1"))

End Sub
I get the result
Book1.xlsb
CDE
115/02/2023
2test
620/02/2023
719/02/2023
926/02/2023
1023/02/2023
1117/02/2023
1318/02/2023
1425/02/2023
1522/02/2023
1724/02/2023
1821/02/2023
2116/02/2023
Sheet1


If I use Format
VBA Code:
Sub Filterit()
   
   Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row).AutoFilter 1, ">" & Format(CLng(Range("E1")), "dd/mm/yyyy")

End Sub
I get
Book1.xlsb
ABCDE
115/02/2023
2test
25
26
27
28
29
30
31
32
33
Sheet1


As the text is being seen as greater than any number
 
Upvote 0
A format is just an appearance of the cell, you want to use it's numeric value.
For example with the data set below (with the dates formatted as dd/mm/yyyy)
Book1.xlsb
CDE
115/02/2023
2test
310/02/2023
409/02/2023
514/02/2023
620/02/2023
719/02/2023
813/02/2023
926/02/2023
1023/02/2023
1117/02/2023
1212/02/2023
1318/02/2023
1425/02/2023
1522/02/2023
1607/02/2023
1724/02/2023
1821/02/2023
1905/02/2023
2006/02/2023
2116/02/2023
2208/02/2023
2311/02/2023
Sheet1

using the code
VBA Code:
Sub Filterit()
 
   Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row).AutoFilter 1, ">" & CLng(Range("E1"))

End Sub
I get the result
Book1.xlsb
CDE
115/02/2023
2test
620/02/2023
719/02/2023
926/02/2023
1023/02/2023
1117/02/2023
1318/02/2023
1425/02/2023
1522/02/2023
1724/02/2023
1821/02/2023
2116/02/2023
Sheet1


If I use Format
VBA Code:
Sub Filterit()
  
   Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row).AutoFilter 1, ">" & Format(CLng(Range("E1")), "dd/mm/yyyy")

End Sub
I get
Book1.xlsb
ABCDE
115/02/2023
2test
25
26
27
28
29
30
31
32
33
Sheet1


As the text is being seen as greater than any number

Okay, so if I double click in the date cells they will then get filtered correctly.
Any thoughts on how I can get them in the right format what I'm entering the value from the userform?
 
Upvote 0
if you put the formula
Excel Formula:
=isnumber(A2)
in a blank cell changing the A2 to the cell reference of one of the cells containing the date do you get FALSE?
 
Upvote 0
On a copy of your workbook (because we are altering data so you don't want to test on the original)

Select your dates
On the Data tab click Text To Columns
Delimited
Click Next
Make sure all the checkboxes are empty
Click Next
Click the Date check box with the option set as DMY
Click Finish

Format the cells as date again if needed

Does the formula return TRUE now for all the cells?
 
Upvote 1
Solution
On a copy of your workbook (because we are altering data so you don't want to test on the original)

Select your dates
On the Data tab click Text To Columns
Delimited
Click Next
Make sure all the checkboxes are empty
Click Next
Click the Date check box with the option set as DMY
Click Finish

Format the cells as date again if needed

Does the formula return TRUE now for all the cells?

That works perfectly. I ended up using the below code.

Thank you very much for your help.

VBA Code:
    wsRD.Cells(j, 6).TextToColumns Destination:=Range("F" & j), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
 
Upvote 0
You're welcome.

Just make sure that you use
VBA Code:
wsRD.Range("F" & j)
rather than
VBA Code:
Range("F" & j)
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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