?
Solved

Excel VBA custom autofilter not finding records

Posted on 2011-02-11
2
Medium Priority
?
298 Views
Last Modified: 2012-05-11
I have a bit of a strange issue which I can't seem to solve.

I have a macro which drives an autofilter from 4 possible filter criteria. 3 of these are a direct population of a variable into the appropriate filter field and work fine, but 1 results in a custom filter and doesn't return any records.

When I look at the filtered sheet I can see that the filter field is active, and when I click on the filter arrow it shows that the custom criteria has been populated with the correct variables, yet the sheet shows 0 records. However, when I click 'OK' in the custom filter popup box, it suddenly works correctly and returns the records which I know are there. ???

I've included an extract of the code. Basically it is intended to check a cell to see if the user has selected a Month or Quarter from the drop down menu. On the sheet this drives a lookup to identify the start and end date for the month/quarter, and the code uses the results of the lookup to set the custom filter to include only those records which are between the start and end of the chosen period. If the user hasn't selected a filter for the period then it is completely ignored.

Thanks for your help Experts, I'm stumped on this one.

Let period_filter = Sheets("Reports").Range("d12").Value
If period_filter <> "" Then
    Let period_start = Sheets("Reports").Range("d13").Value
    Let period_end = Sheets("Reports").Range("e13").Value
    Selection.AutoFilter Field:=6, Criteria1:=">=" & period_start, Operator:=xlAnd _
    , Criteria2:="<=" & period_end
Else
End If

Open in new window

0
Comment
Question by:pendulum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 34872793
Try:
Let period_filter = Sheets("Reports").Range("d12").Value
If period_filter <> "" Then
    Let period_start = Sheets("Reports").Range("d13").Value
    Let period_end = Sheets("Reports").Range("e13").Value
    Selection.AutoFilter Field:=6, Criteria1:=">=" & clng(period_start), Operator:=xlAnd _
    , Criteria2:="<=" & clng(period_end)
Else
End If

Open in new window

0
 

Author Closing Comment

by:pendulum
ID: 34872850
Awesome, thanks for your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question