+ Reply to Thread
Results 1 to 3 of 3

Filter_VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    03-26-2012
    Location
    New Jersey, US
    MS-Off Ver
    Excel 2010
    Posts
    22

    Filter_VBA

    Evening,

    I am trying to learn the VBA for creating filters. For example on the attached spreasheet, column Q has various 4 digit #'s, but I would like to know a macro that filters only 5601, 5801, 6001, 6201, 6301, 7701, 8801, 9201, 9501. I have utilized the record macro to get the below code:

    But the issue is the range. The range of data may change from week to week, it will not always end at row 327. How would i adjust the code so it will look at all the data that is in column Q (which again, could change from week to week) yet still filter only what I want?

    Selection.AutoFilter
        ActiveSheet.Range("$Q$1:$Q$327").AutoFilter Field:=1, Criteria1:=Array( _
            "5601", "5801", "6001", "6201", "6301", "7701", "8301", "8801", "9201", "9501"), _
            Operator:=xlFilterValues
    End Sub
    Thanks so much for the guidance!
    Alec
    Attached Files Attached Files
    Last edited by vlady; 01-28-2013 at 01:18 AM.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Filter_VBA

    Some thing like this..........

    Sub Filter_test()
    Dim rAfArea As Range, vAfField As Variant, sAfHeader As String
    
    sAfHeader = "DC"
    Set rAfArea = Range("A1").CurrentRegion
    vAfField = Application.Match(sAfHeader, rAfArea.Rows(1), 0)
    
    If TypeName(vAfField) = "Error" Then
        MsgBox "Filter Column Not Found", vbCritical, "Missing Column Heading"
        Exit Sub
    End If
        
    rAfArea.AutoFilter Field:=vAfField, Criteria1:= _
        Array("5601", "5801", "6001", "6201", "6301", "7701", "8301", "8801", "9201", "9501"), _
        Operator:=xlFilterValues
    
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    03-26-2012
    Location
    New Jersey, US
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Filter_VBA

    Yes, that indeed worked. Thanks so much for the assistance.

+ 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