+ Reply to Thread
Results 1 to 16 of 16

Auto Filter controled by a macro - simple question!

  1. #1
    Registered User
    Join Date
    08-06-2007
    Posts
    10

    Question Auto Filter controled by a macro - simple question!

    All,

    I'm trying to set up a macro to to run and control the AutoFilter in my Excel Spread Sheet. Essentially, all i want to be able to acheive is, instead of inputting the cryteria myself in the auto filter, i want the macro to select the cryteria from a specific cell.

    Example...

    Running the autofilter, normally you would select "equals or grater than" option and you would input a figure then click the "or" option then input "equals or less than" and a new figure and then click ok. This would sort the range; see the example macro...

    ' test1 Macro
    ' Macro recorded 06/08/2007 by Nigel M Bailey
    '
    Selection.AutoFilter Field:=10, Criteria1:=">=01/09/207", Operator:=xlOr _
    , Criteria2:="<=31/09/2007"
    ActiveCell.Offset(774, -1).Range("A1").Select
    ActiveWindow.SmallScroll Down:=-15
    End Sub

    What i want to beable to do is to run a macro which will do the above but instead or requiring the user to input the range cryteria for the filter it would point to a cell to get the value from.

    ' test1 Macro
    ' Macro recorded 06/08/2007 by Nigel M Bailey
    '
    Selection.AutoFilter Field:=10, Criteria1:="> Cell A2", Operator:=xlOr _
    , Criteria2:="< Cell A3"
    ActiveCell.Offset(774, -1).Range("A1").Select
    ActiveWindow.SmallScroll Down:=-15
    End Sub

    In doing this i can validate the selection field and just add a search button which then will be perfect!

    Is this possible to do?
    Last edited by NigelMBailey; 08-06-2007 at 09:21 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Something like

    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    Thanks; this sort of works!

    the problem is that it now reverses one of the dates>!?!?!

    The Cells are

    01/09/2007and 30/09/2007

    In the filter it does this...

    09/01/2007 and 30/09/2007

    Not sure why as i have validated the dates across bothcells...

    Any ideas?

  4. #4
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    Help - Any Help>?

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Can you attach a small example?

  6. #6
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    I cant really as it is just filling out the box and reversing the date in the custom section of the autofilter box....

    I am bemused!

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Do you mean the date format is changing?

  8. #8
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    yes, but only in one field!

    The first is 01/09/2007
    the second is 09/01/2007

    The actual cells are validated a the correct dd/mm/yyyy

    Not sure why it is doing it...

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    That makes the results consistant but reverses both fields!

    months to days and days to months!

  11. #11
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    ok - ve checked this out by validating the date field to a number and then re running the macro and it appears that it is not pulling the values from the stated cells and populating it with rubbish; hence it not filtering properly...

    This is what i have now...

    Sub searchfields()
    '
    ' test1 Macro
    ' Macro recorded 06/08/2007 by Nigel M Bailey
    '
    '
    Selection.AutoFilter Field:=10, Criteria1:=">=" & CDate(Range("N8")), Operator:=xlAnd _
    , Criteria2:="<=" & CDate(Range("O8"))
    End Sub

  12. #12
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    Progress!

    Now i have fixed the input cells and they are beig carried forward to the autofilter selection cryteria sheet. However it is showing nothing. But if i look at the custom filter it is showing the correct values in each and when i just click ok it then shows the correct values filtered......

    How can it force a recalculate in the macro.>?

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Quote Originally Posted by NigelMBailey
    Progress!

    Now i have fixed the input cells and they are beig carried forward to the autofilter selection cryteria sheet. However it is showing nothing. But if i look at the custom filter it is showing the correct values in each and when i just click ok it then shows the correct values filtered......

    How can it force a recalculate in the macro.>?
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    Bizzare!

    No change - it calculates properly but you have to look at the custom cryteria on the filter and click "ok" for it to actually filter correctly...

    Sub searchfields()
    '
    ' searchfields Macro
    ' Macro recorded 06/08/2007 by Nigel M Bailey
    '
    '
    If ActiveSheet.FilterMode Then
    ActiveSheet.ShowAllData
    End If
    Selection.AutoFilter Field:=10, Criteria1:=">=" & CDate(Range("N8")), Operator:=xlAnd _
    , Criteria2:="<=" & CDate(Range("O8"))
    Selection.Calculate
    End Sub

    I'm lost...

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I don't know what this might be, possibly if you attach the workbook I might get a better idea.

  16. #16
    Registered User
    Join Date
    08-06-2007
    Posts
    10
    unfortunately it is too big - can i e-mail you direct? Is that too cheaky?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1