Setting a date filter as a variable in VBA

Davek81

New Member
Joined
Aug 19, 2014
Messages
7
Hi All, I have some code that filters a range of data based on a unique number and the last 7 days. What I would like to do is be able to change the 7 days and put it as a variable so that it can be changed to whatever the user specifys. I am not sure on the syntax but basically i want the code to say filter based on todays date minus whatever is in cell "x"

This is my code, i have highlighted the required variable

.AutoFilterMode = False
.UsedRange.AutoFilter
.UsedRange.AutoFilter field:=1, Criteria1:="D5060"
.UsedRange.AutoFilter field:=2, Criteria1:="<=" & CLng(Date), Operator:=xlAnd, _
Criteria2:=">=" & CLng(Date - 7)

Thanks in advance
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi,
try changing the line of your code to this:

Code:
CLng(Date - CInt(Range("X1").Value))

If the worksheet with variable value is not active when code run then you will need to qualify the range.
Adjust range as required.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,604
Messages
6,173,320
Members
452,510
Latest member
RCan29

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