Filter date based on todays date

behedwin

Active Member
Joined
Dec 10, 2014
Messages
399
Hey

I use this code to filter data in a combobox. It works great!!

Code:
Sub FilterPersonellListBox_Larare()
    Dim ws As Worksheet
    Dim lr As Long
    Dim FiltRng As Range
    Dim R As Range
Personalform.ListBox_listapersonal.Clear    'clear listbox
Set ws = Sheets("REGISTER")
Personalform.ListBox_listapersonal.ColumnCount = 4
Personalform.ListBox_listapersonal.ColumnWidths = "1;70;70;55"


With ws
    lr = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    Set FiltRng = Sheets("REGISTER").Range("N3:N" & lr)
    
    FiltRng.AutoFilter Field:=14, Criteria1:="Lärare"

    
    On Error Resume Next
    For Each R In FiltRng.Offset(1).SpecialCells(xlCellTypeVisible).Cells
        With Personalform.ListBox_listapersonal
            .AddItem
            .List(.ListCount - 1, 0) = ws.Cells(R.Row, "AL")
            .List(.ListCount - 1, 1) = ws.Cells(R.Row, "B")
            .List(.ListCount - 1, 2) = ws.Cells(R.Row, "C")
            .List(.ListCount - 1, 3) = ws.Cells(R.Row, "R")
        End With
    Next R
    On Error GoTo 0

    FiltRng.AutoFilter
    
End With
End Sub

However...
Now i want to use this line

Code:
    FiltRng.AutoFilter Field:=14, Criteria1:="Lärare"

And filter a date compared to todays date.

The question is this:

In column X i have dates of when my staff contracts are ending.
I want to filter out all dates in column X that are between todays date and 3 months ahead.
How can i do this?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Possibly...

Code:
FiltRng.AutoFilter Field:=14, Criteria1:= _
        ">=" & CLng(Date), Operator:=xlAnd, Criteria2:="<=" & CLng(DateAdd("m", 3, Date))

Although I don't see how Field:=14 works when FiltRng is a single column :confused:
 
Last edited:
Upvote 0
You're welcome but very strange it works as Field:=14 should make it error as it should only be Field:=1 when the range is a single column.
 
Upvote 0
You're welcome but very strange it works as Field:=14 should make it error as it should only be Field:=1 when the range is a single column.
If the sheet is already filtered on all data, then it will work.
 
Upvote 0
If the sheet is already filtered on all data, then it will work.

Yes, good point although there is nothing in the code to indicate that it is (and a strange way of going about it as the OP removes the filter later in the code), but then I can't see what the OP is doing with the sheet so as long as they are happy then all good :biggrin:

Thanks for the input.
 
Upvote 0
Yes, good point although there is nothing in the code to indicate that it is (and a strange way of going about it as the OP removes the filter later in the code), but then I can't see what the OP is doing with the sheet so as long as they are happy then all good :biggrin:

Thanks for the input.
I agree that it seems a bit strange, but as you said, if it's just the 1 column being filtered there should be an error.
 
Upvote 0
Yes, good point although there is nothing in the code to indicate that it is (and a strange way of going about it as the OP removes the filter later in the code), but then I can't see what the OP is doing with the sheet so as long as they are happy then all good :biggrin:

Thanks for the input.

Yes, it seems very strange that it worked since the dates are in column X (???)

From post 1
The question is this:

In column X i have dates of when my staff contracts are ending.
I want to filter out all dates in column X that are between todays date and 3 months ahead.
How can i do this?

But, as you've said ..."so as long as they are happy then all good"

M.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,172
Members
448,870
Latest member
max_pedreira

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