Consulting

Results 1 to 3 of 3

Thread: Solved: AutoFilter question

  1. #1

    Solved: AutoFilter question

    Consider the following code snippet...

    [vba]call ActivateWorksheet("someSheet")
    call SetFilters(22, "someValue")

    Sub ActivateWorksheet(sSheet)
    If ActiveSheet.Name <> sSheet Then
    Worksheets(sSheet).Activate
    End If
    End Sub

    Sub SetFilters(nField, cCriteria)
    Selection.AutoFilter Field:=nField, Criteria1:=cCriteria
    End Sub[/vba]

    The above code snippets work just fine, however I want to avoid activating the particular worksheet, so I thought that I could do something like this...

    [vba]Sub SetFilters(sSheet, nField, cCriteria)
    Worksheets(sSheet).AutoFilter Field:=nField, Criteria1:=cCriteria
    End Sub[/vba]
    Simply put, why isn't activating a particular worksheet followed by the Selection.AutoFilter functionally equivalent to straight-out referencing it using Worksheets("someSheet").AutoFilter.?

    Is there an easy way to (easily) accomplish what I want w/o a ton of complex coding?

    Thank you!

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    The difference I see in your code is that in one case Autofilter is qualified with a range (Selection), and in the other by a worksheet. So if you were to include a reference to a range in your 2nd version of SetFilters would that do the trick? A guess. Untested.[vba]Worksheets(sSheet).range("AB454").AutoFilter Field:=nField, Criteria1:=cCriteria
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Thanks for the explanation, however I still don't understand how "Selection" understands the location of the autofilter after ... nonetheless, here's my hack to solve the problem...

    [vba]Worksheets(sSheet).Range("B9:FC9").AutoFilter Field:=iField, Criteria1:=sCriteria[/vba]

Posting Permissions

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