VBA AdvanceFilter with rangecriteria as a range that contains fields with text and dates not working

marcnz

New Member
Joined
Dec 19, 2012
Messages
13
Hi,

I am pulling my hair out with this issue.

I have a source sheet containing all the data to be filtered.
I have a destination sheet containing the first 3 rows for the filter criteria range set up and from row 4 the results to be copied to including the header and data from the source sheet.
I have it working as long as I don't set any dates in the filter criteria.
As soon as I have dates as criteria, nothing is copied over.

My source file has column E containing the starting date, column F containing the ending date, and the column I containing a product type (string).

I set up the filter criteria data via VBA, ie: destination sheet (called "ForecastFilter") like the code below:

Case "AccMth"

.Range("E2").Value = ">=" & Range("startdate")
.Range("F2").Value = "<=" & Range("enddate")
.Range("I2").Value = "Product A"
.Range("J2").Value = ""
.Range("K2").Value = ""

'Set the filter criteria range according to entries
.Range(.Range("E1"), .Range("I2")).Name = "FilterCriteria2"

.Range refers to the destination sheet "ForecastFilter". The workbook as a few range names. Two are used to contain the startdate and enddate from another sheet in normal date format (not US format...)

PS. I tried with ">=1/4/2013" and "<=30/4/2013" instead of the range("startdate") and range("enddate") to be sure! No luck either.

Then the "Update" button on a third final destination sheet ("Account Monthly") should receive a specific range from the filtered data that were copied in the "ForecastFilter" sheet based on the criteria set, using a pivot table (that is not working either but it is not the question here).

If I don't include the dates in my filter criteria, but "Product A" only I have the correct rows copied over into the "ForecastFilter" sheet. As soon as I add any date in the criteria, nothing shows up below the headings.

The range "FilterCriteria2" is being updated at runtime with the information above. Information varies depending what sheet is calling for the data to be filtered from the source "ForecastData".

Here is the code that should copy data from source "ForecastData" to the filtered data sheet "ForecastFilter":

'AdvancedFilter data by chosen criteria
lastRow = ThisWorkbook.Worksheets("ForecastData").Cells(Rows.Count, "A").End(xlUp).Row
ThisWorkbook.Worksheets("ForecastData").Range("A3:EB" & lastRow).AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Range("FilterCriteria2"), _
CopyToRange:=ThisWorkbook.Worksheets("ForecastFilter").Range("A5"), _
Unique:=False


If I remove the "CriteriaRange" line, all comes through unfiltered. If I leave the criteria value for "E2" and "F2" as empty, I have all data for "Product A" coming through, so the filter on the text is working as expected.

I can't find anything that could help me solve this issue.

The file was originally created in Excel 2003 and is now being used with Excel 2010. Everything was working fine in 2003... Not sure why it stopped working when using Excel 2010.
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Does this work?
Code:
.Range("E2").Value = ="">=""&startdate"
.Range("F2").Value = =""<=""&enddate"
 
Upvote 0
Hi,

I found the solution to my problem. I had to convert the dates to as a long data type.

Finally got it working as intended!

Thanks a lot for trying to help.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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