filter by month using VBA

snowman1976

Board Regular
Joined
Nov 4, 2008
Messages
191
hello
I have this simple bit of a macro that I use to filter information based on a date the user inputs into cell H1.
This works fine, but I am currently using formula's in A1 and B1 as 'helpers' to my macro. I would like to see what can be changed so A1 and B1 can be eliminated, and H1 is the only one it needs. Right now H1 is just the user inputting a date so I know what month they are looking for - so in other words if you input any date in April the range will become 4/1/2016 to 4/30/2016

cell A1=DATE(YEAR(H1),MONTH(H1),1)
cell B1=DATE(YEAR(H1),MONTH(H1)+1,1)-1


the code is as follows. Does anyone know how I can get the same result without the two helping cells?
Any help would be appreciated

startdate = Sheets("master").Range("a1")
EndDate = Sheets("master").Range("b1")

Criteria1 = ">=" & startdate
Criteria2 = "<=" & EndDate
Sheets("PROD_ACT").Select
ActiveSheet.Range("A:O").AutoFilter Field:=5, Criteria1:=Criteria1 _
, Operator:=xlAnd, Criteria2:=Criteria2

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi,
give following a try & see if does what you want.

Code:
    Dim StartDate As Long, EndDate As Long
    
    With Sheets("master").Range("H1")
        StartDate = DateSerial(Year(.Value), Month(.Value), 1)
        EndDate = DateSerial(Year(.Value), Month(.Value) + 1, 0)
    End With


    
    Sheets("PROD_ACT").Range("A:O").AutoFilter Field:=5, _
                                                Criteria1:=">=" & StartDate, _
                                                Operator:=xlAnd, _
                                                Criteria2:="<=" & EndDate

Dave
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,061
Members
449,206
Latest member
Healthydogs

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