autofilter <= not working

GlennW81

New Member
Joined
Aug 19, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to filter two columns, one an effective date, the second an expiry date, to filter permits that were valid for a particular year. As the year of interest changes I have set up a userform which I can enter the year of interest.
I am successfully able to get the first filter >= to work, however, the filter for the second columns does not work that contain <=.
I can get the second filter to work if I remove the < and just have equal t. But this is not ideal as I can not filter the less than dates.
I have also tried having two criteria for the same column, criteria1 equals to, and criteria less than, with the Operator:=xlfiltervalues, though this does not work either as.
When I say it does not work, the result of the filter is that no data selected/shown in the sheet, even though I can clearly see data that should be selected in the filter and shown.
Why does >= work correctly but <= does not?

The following is the line of code I refer to:

VBA Code:
Public HPyr As Long
Dim wsHPHarv As Worksheet
Dim lastrow, lcol As Long

Sub org_Harv_Permit_sht()

Set wsHPHarv = ThisWorkbook.Worksheets("HP harvester permits")
lastrow = wsHPHarv.Range("B" & Rows.Count).End(xlUp).Row
lcol = wsHPHarv.Cells(1, Columns.Count).End(xlToLeft).Column

wsHPHarv.Range(wsHPHarv.Cells(1, 1), wsHPHarv.Cells(lastrow, lcol)).AutoFilter field:=5, Criteria1:=">=1/01/" & HPyr
wsHPHarv.Range(wsHPHarv.Cells(1, 1), wsHPHarv.Cells(lastrow, lcol)).AutoFilter field:=6, Criteria1:="<=31/12/" & HPyr

End Sub

Really appreciate any help.

Cheers
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Where do you set the variable HPyr? If you don't set its value, it will be 0, which might explain why the first filter works, but not the second.
 
Upvote 0
Hi @kevin9999 , If it was 0 I don't think it would work on the first line either. The variable HPyr gets its value from input in textbox of userform. I have stepped through the code to ensure the value gets passed to the module fine. For example, user inputs 2022, then HPyr is set to 2022.

I have discovered the issue appears to be the date settings in VBA. Autofilter criteria date seems to only work when the date format in the code is American date. My data (being an Australian) in the spreadsheet is in dd/mm/yyyy format. However, the criteria on the autofilter appears to be default to mm/dd/yyyy. Because 1/1 is considered 1st of Jan in both formats it works fine. Though the second line does not filter because the
VBA Code:
Criteria1:="<=31/12/" & HPyr
considers the 31 as the month.
Is there a way to change the criteria settings? Or am I just stuck with having to remember to use the mm/dd format in the autofilter criteria?

Cheers,
Glenn
 
Upvote 0
Dates are always a bit tricky with AutoFilter. Try the following (adjust as you see fit).

This Code
VBA Code:
Option Explicit
Sub GlennW81()
    Dim ws As Worksheet, HPyr As Long
    Set ws = ThisWorkbook.Worksheets("HP harvester permits")
    
    'I'm getting HPyr from the sheet - you'll get it from your userform
    HPyr = ws.Range("H1")
    
    With ws.Cells(1, 1)
        .AutoFilter 5, ">=" & CLng(DateValue("1/01/" & HPyr))
        .AutoFilter 6, "<=" & CLng(DateValue("31/12/" & HPyr))
    End With

End Sub

Turns this
test.xlsm
ABCDEFGH
1Header1Header2Header3Header4Header5Header62022
201/01/202101/01/2021
315/06/202115/06/2021
401/01/202201/01/2022
501/06/202201/06/2022
630/12/202230/12/2022
731/12/202231/12/2022
801/01/202301/01/2023
901/06/202301/06/2023
1001/06/202201/06/2022
1130/12/202230/12/2022
12
HP harvester permits


Into this
test.xlsm
ABCDEFGH
1Header1Header2Header3Header4Header5Header62022
401/01/202201/01/2022
501/06/202201/06/2022
630/12/202230/12/2022
731/12/202231/12/2022
1001/06/202201/06/2022
1130/12/202230/12/2022
12
HP harvester permits


Is that what you want?
 
Upvote 0
Another way...

VBA Code:
Sub GlennW81_2()
    Dim ws As Worksheet, HPyr As Long
    Set ws = ThisWorkbook.Worksheets("HP harvester permits")
    
    'I'm getting HPyr from the sheet - you'll get it from your userform
    HPyr = ws.Range("H1")
    
    With ws.Cells(1, 1)
        .AutoFilter 5, ">=" & DateSerial(HPyr, 1, 1)
        .AutoFilter 6, "<=" & DateSerial(HPyr, 31, 12)
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,910
Messages
6,181,675
Members
453,061
Latest member
schiefA

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