+ Reply to Thread
Results 1 to 3 of 3

Set autofilter criteria with VBA from a range

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2012
    Location
    IL
    MS-Off Ver
    Excel 2010
    Posts
    50

    Set autofilter criteria with VBA from a range

    I'm trying to set the criteria for an autofilter using VBA. I would like the values to come from a list on a separate sheet. So I have the autofilter on Sheet1 A:K and would like to set the criteria in column A to match all entries in column A of sheet 2, which will be a dynamic range. This is what I have pieced together and can't seem to get any further.

    Sub test()
    
    Dim rng As String
    Dim lastrow As Long
    Dim lastcell As Range
    Dim myrange As Range
    
    Set myrange = Sheet2.Range("A:A")
    
    lastrow = Cells(100000, myrange.Column).End(xlUp).Row
    lastcell = Cells(lastrow, 1)
    
    For Each cell In Sheet2.Range("A2:" & lastcell).Cells.SpecialCells(xlCellTypeVisible)
    rng = rng & Chr(34) & cell.Value & Chr(34) & ","
    
    
    Next
    
    Sheet1.Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("A:K").AutoFilter Field:=1, Criteria1:=Array( _
            rng), Operator:=xlFilterValues
    
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Set autofilter criteria with VBA from a range

    Try this:

    Sub Filter_Range()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1")
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet2")
    Dim arr As Variant, arrVal() As String
    Dim bDim As Boolean:    bDim = False
    Dim i As Long
    
    arr = ws2.Range("A1:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
    
    For i = LBound(arr, 1) To UBound(arr, 1)
        If bDim = False Then
            ReDim arrVal(0 To 0) As String
            arrVal(0) = arr(i, 1)
            bDim = True
        Else
            ReDim Preserve arrVal(0 To UBound(arrVal) + 1) As String
            arrVal(UBound(arrVal)) = arr(i, 1)
        End If
    Next i
    
    With ws1
        .AutoFilterMode = False
        .Range("A1:K" & .Range("A" & Rows.Count).End(xlUp).Row).AutoFilter 1, arrVal, xlFilterValues
    End With
        
    End Sub
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Registered User
    Join Date
    05-22-2012
    Location
    IL
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: Set autofilter criteria with VBA from a range

    Works perfectly! Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] AutoFilter - Criteria - Array - Range - Not working please assist.
    By kd7 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-22-2015, 08:34 AM
  2. [SOLVED] Using an array from a range for Autofilter Criteria
    By J-Tin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2013, 10:49 AM
  3. [SOLVED] Autofilter range criteria filter only last cell value in range
    By SAGAR KHOLLAM in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-01-2013, 09:00 AM
  4. Replies: 4
    Last Post: 03-16-2013, 08:33 AM
  5. Replies: 2
    Last Post: 07-06-2012, 11:42 AM
  6. Macro, autofilter, delete rows outside date range, defined names as criteria
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2011, 04:10 AM
  7. AutoFilter method of Range class failed - Yet autofilter works.
    By Carlsbergen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2009, 05:43 PM

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