'Object required when trying to make variable Autofilter
Making a auto filter that relies on a range in another (I've named) in another sheet that can be applied by vba. I thought it was not over the top, but it keeps saying object required? if someone could help I would appreciate it :)
Sub AutoFilter_Test()
Dim vCrit As Variant
Dim wsO As Worksheet
Dim wsL As Worksheet
Dim rngCrit As Range
Dim rngOrders As Range
Set wsO = Worksheets("CA Orders")
Set wsL = Worksheets("Kitt Codes")
Set rngOrders = wsO.Range("$A$1").CurrentRegion
Set rngCrit = wsL.Range("CritList")
vCrit = rngCrit.Value
rngOrders.AutoFilter Field:=2, Criteria1:=Applications.Transpose(vCrit), Operator:=xlFilterValues
Solution 1:[1]
Filter on Multiple Values (Strings)
Option Explicit
Sub AutoFilter_Test()
Dim wsO As Worksheet: Set wsO = Worksheets("CA Orders")
Dim wsL As Worksheet: Set wsL = Worksheets("Kitt Codes")
Dim rngOrders As Range: Set rngOrders = wsO.Range("$A$1").CurrentRegion
Dim rngCrit As Range: Set rngCrit = wsL.Range("CritList")
Dim Data As Variant
Data = rngCrit.Value ' assuming there is more than one cell.
Dim rCount As Long: rCount = UBound(Data, 1)
Dim cCount As Long: cCount = UBound(Data, 2)
Dim vCrit() As String: ReDim vCrit(0 To rCount * cCount - 1)
Dim n As Long: n = -1
Dim Key As Variant
Dim r As Long, c As Long
For r = 1 To UBound(Data, 1)
For c = 1 To UBound(Data, 2)
Key = Data(r, c)
If Not IsError(Key) Then
If Len(Key) > 0 Then
Key = CStr(Key)
n = n + 1
vCrit(n) = Key
End If
End If
Next c
Next r
Erase Data
If n = -1 Then Exit Sub
ReDim Preserve vCrit(0 To n)
rngOrders.AutoFilter Field:=2, Criteria1:=vCrit, Operator:=xlFilterValues
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | VBasic2008 |
