Consulting

Results 1 to 9 of 9

Thread: Solved: Variable as AutoFilter Criteria

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location

    Solved: Variable as AutoFilter Criteria

    I'm using Excel 2000 (old school), and am wondering if it is possible to use a variable as a filtering criteria in AutoFilter.

    Here's an example of what I wrote, but it doesn't work...

    [VBA]Sub CurrMo_Bookings()
    ' Applies AutoFilter to DataXfr worksheet for Bookings.
    Application.ScreenUpdating = False
    ' Clears all Filters
    Call Clear_DataXfr_Filters

    ' Filters for Bookings
    Dim CurrDate As Integer

    CurrDate = InputBox("What is the current date (YYmm)?", "User Input")
    Worksheets("DataXfr").Activate
    Selection.AutoFilter Field:=23, Criteria1:=CurrDate

    Range("B3").Select
    End Sub[/VBA]

    This is a bit crude, since the MessageBox comes up every time. I was just toying with it to see if I could get the AutoFilter to run properly.

    DataXfr is a sheet where I load up all the data from various sheets in the workbook. When I ran the code, it filtered the date field for CurrDate, not 0911 (as would be the case now).

    Thanks
    Last edited by Bob Phillips; 12-03-2009 at 10:30 AM. Reason: Add VBA Tags

  2. #2
    VBAX Regular
    Joined
    Oct 2009
    Location
    Fremont, CA
    Posts
    72
    Location

    Some suggestions

    You declared CurrDate as Integer. Therefore if you were to enter '0911' in the InputBox VBA would interpret it as 911. What format does Field 23 actually have? Date, number or string?

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Dates are fiddly, can you post a simple sample of the data?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location
    Here's a copy of some of the data in Field 23:

    BookMonth0611031205040603060605050701

    The column is formatted as 0000, which I presume could be interpreted as an Integer. This was necessary in my setup as the YYMM format would be 912, instead of the desired 0912 for this month.

    So I ran the code again, and Field 23 had a custom value = 911

    I guess my question has now changed. How would you re-write my code such that the variable carries a format of 0000 to match the values in the target column?

  5. #5
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location
    Sorry, my post was a column when I wrote it, but a buch of gibberish after I posted it. Here's what it looks like, with "Book Month" as the column header.

    Book
    Month
    0611
    0312
    0504
    0603
    0606
    0505
    0701

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a sample workbook. I don't know if your dates are numbers, proper dates or even text.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location
    Can't post the workbook. Column is formatted as a custom number, 0000.

    How do you force a format of 0000 on a variable?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA] CurrDate = InputBox("What is the current date (YYmm)?", "User Input")
    CurrDate = Format(CurrDate, "0000")
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    Aug 2006
    Posts
    36
    Location
    Thanks so much ... this worked!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •