VBA Filter Issue - Fails to filter

coates

New Member
Joined
Aug 4, 2010
Messages
3
Hi,
So i have quite a large script which is extracting data from a scheduling software program into excel. I than run a filter on the activities, to show only the activities with are occuring in the next 5 weeks.

I have been able to get the script to run perfectly when typing in the start date and the date 5 weeks from then into the script. However, as I have to run this macro often, I want to be able to change the two respective dates in a excel cell, than run the macro.

An extract of my script is:

Date1 = Worksheets("5 Week Outlook").Cells(2, 13).Value
Date2 = Worksheets("5 Week Outlook").Cells(2, 14).Value

Set OPProject = OPP.Projects("CURRENT ISSUE")
showdialog = True
OPProject.TimeAnalyze showdialog
Set OPPView = OPProject.views

OPP.ApplyFilter ("Five_Week_Look_Ahead")
Set OPPAct = OPProject.activities
OPPAct.SetFilterTo "Five_Week_Look_Ahead", "(ASDATE NOT_EMPTY and AFDATE IS_EMPTY) or (BSDATE >= 'Date1' AND BSDATE <= 'Date2') or (ESDATE >= 'Date1' AND ESDATE <= 'Date2')"

ActTotal = OPPAct.Count


Rather than using Date1 and Date2, if I simply type in my filter BSDATE >= '05Aug10', the filter runs fine and the 'ActTotal' is around 400, which is what I want.

However, if i use the Date1 and Date2, which is taking the dates from the excel cells, my 'ActTotal' increases to 2407, which is all of the activities. therefore it is not filtering anything.

Any help would be greatly appreciated.
Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi,
So i have quite a large script which is extracting data from a scheduling software program into excel. I than run a filter on the activities, to show only the activities with are occuring in the next 5 weeks.

I have been able to get the script to run perfectly when typing in the start date and the date 5 weeks from then into the script. However, as I have to run this macro often, I want to be able to change the two respective dates in a excel cell, than run the macro.

An extract of my script is:

Date1 = Worksheets("5 Week Outlook").Cells(2, 13).Value
Date2 = Worksheets("5 Week Outlook").Cells(2, 14).Value

Set OPProject = OPP.Projects("CURRENT ISSUE")
showdialog = True
OPProject.TimeAnalyze showdialog
Set OPPView = OPProject.views

OPP.ApplyFilter ("Five_Week_Look_Ahead")
Set OPPAct = OPProject.activities
OPPAct.SetFilterTo "Five_Week_Look_Ahead", "(ASDATE NOT_EMPTY and AFDATE IS_EMPTY) or (BSDATE >= 'Date1' AND BSDATE <= 'Date2') or (ESDATE >= 'Date1' AND ESDATE <= 'Date2')"

ActTotal = OPPAct.Count


Rather than using Date1 and Date2, if I simply type in my filter BSDATE >= '05Aug10', the filter runs fine and the 'ActTotal' is around 400, which is what I want.

However, if i use the Date1 and Date2, which is taking the dates from the excel cells, my 'ActTotal' increases to 2407, which is all of the activities. therefore it is not filtering anything.

Any help would be greatly appreciated.
Thanks
you've defined date1 and date2 as variables. Try removing the ' marks:
Code:
or (BSDATE >= Date1 AND BSDATE <= Date2) or (ESDATE >= Date1 AND ESDATE <= Date2)
 
Upvote 0
you've defined date1 and date2 as variables. Try removing the ' marks:
Code:
or (BSDATE >= Date1 AND BSDATE <= Date2) or (ESDATE >= Date1 AND ESDATE <= Date2)

I have removed the ' marks and it is still the same result. Would it be something to do with the format of the excel cells. when my filter worked by simply typing '05Aug10' etc is that using a string??
Because when I use Date1 it might be using a different format.
When I step through my code, Date1 and Date2 both depict the correct date as "09Aug10" and "24Sep10". I have tried changing the cell format to text but that didnt not work either. What about Cells(2,13).Value? Is the .Value correct?

thanks again
 
Upvote 0
I have removed the ' marks and it is still the same result. Would it be something to do with the format of the excel cells. when my filter worked by simply typing '05Aug10' etc is that using a string??
Because when I use Date1 it might be using a different format.
When I step through my code, Date1 and Date2 both depict the correct date as "09Aug10" and "24Sep10". I have tried changing the cell format to text but that didnt not work either. What about Cells(2,13).Value? Is the .Value correct?

thanks again
The .Value is ok. The only other thing that might be worth a try is to pull the dates out of the " marks - something like this:
Code:
"(ASDATE NOT_EMPTY    and  AFDATE IS_EMPTY) or (BSDATE >=" & Date1 &  "AND  BSDATE <=" & Date2 & ") or (ESDATE >=" & Date1 & "AND ESDATE <=" & Date2 & ")"
 
Upvote 0
The .Value is ok. The only other thing that might be worth a try is to pull the dates out of the " marks - something like this:
Code:
"(ASDATE NOT_EMPTY    and  AFDATE IS_EMPTY) or (BSDATE >=" & Date1 &  "AND  BSDATE <=" & Date2 & ") or (ESDATE >=" & Date1 & "AND ESDATE <=" & Date2 & ")"

That didnt change it either sorry. I think the issues lays in that I dont fully understand how the original version worked.
i.e Typing BSDATE >= '05Aug10'
Does VBA recogonise it as a date without me implying that? Or is it simpy a string and it is looking in the BSDATE column of my data for activities greater than this string? If that makes sense...
If it is actually a date, how do I replicate my Date1 and Date2 to be of the same format as simply typing it in?
 
Upvote 0

Forum statistics

Threads
1,211,804
Messages
6,104,076
Members
447,893
Latest member
Hoosier605

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