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
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,111
Office Version
365, 2010
Platform
Windows
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)
 

coates

New Member
Joined
Aug 4, 2010
Messages
3
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
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,111
Office Version
365, 2010
Platform
Windows
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 & ")"
 

coates

New Member
Joined
Aug 4, 2010
Messages
3
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?
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,873
Messages
5,513,920
Members
408,977
Latest member
Cattle

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top