Estoy usando el siguiente código para establecer una matriz en los valores de un rango con nombre (que contiene números de cuenta) y luego uso esa matriz como criterio para autofiltrar una tabla (Tabla 1).
Sub FilterRangeCriteria() Dim vCrit As Variant Dim rngCrit As Range Set rngCrit = wsL.Range("CritList") vCrit = rngCrit.Value wsO.ListObjects("Table1").Range.AutoFilter _ Field:=8, _ Criteria1:=vCrit, _ Operator:=xlFilterValues End SubPuedo ver que la matriz contiene todos los valores del rango con nombre; sin embargo, la tabla que intento filtrar eliminará todas las filas debajo del encabezado y no mostrará ninguna fila con los criterios del rango con nombre.
Esto funcionará si CritList es una sola columna o fila. De lo contrario, deberá crear una matriz 1D a partir de los valores.
Sub FilterRangeCriteria() Dim vCrit As Variant Dim rngCrit As Range Set rngCrit = wsL.Range("CritList") vCrit = WorksheetFunction.Transpose(WorksheetFunction.Unique(rngCrit)) wsO.ListObjects("Table1").Range.AutoFilter _ Field:=8, _ Criteria1:=vCrit, _ Operator:=xlFilterValues End Sub Sub FilterRangeCriteria() ' Source - read criteria list ' 'wsL' is the code name of a worksheet containing this code Const sRangeName As String = "CritList" ' Destination - filter a table ' 'wsO' is the code name of a worksheet containing this code Const dtblName As String = "Table1" Const dlcName As String = "Ledger Account" ' Keep in mind that this would fail on 'UBound(cData, 1)' ' if there was only one cell in the named range. Dim cData As Variant: cData = wsL.Range(sRangeName).Value ' "AutoFilter" 'likes' strings and the array needs to be 1D, ' hence write the values from the 2D one-based one-column criteria array, ' converted to strings, to a 1D one-based string array ('sArr'). Dim sArr() As String: ReDim sArr(1 To UBound(cData, 1)) Dim r As Long For r = 1 To UBound(cData, 1) sArr(r) = CStr(cData(r, 1)) Next r ' Reference the destination table. Dim dtbl As ListObject: Set dtbl = wsO.ListObjects(dtblName) Application.ScreenUpdating = False ' Remove any filters. If dtbl.ShowAutoFilter Then If dtbl.AutoFilter.FilterMode Then dtbl.AutoFilter.ShowAllData Else dtbl.ShowAutoFilter = True End If ' Reference the destination table criteria column. Dim dlc As ListColumn: Set dlc = dtbl.ListColumns(dlcName) ' Apply the filter. dtbl.Range.AutoFilter dlc.Index, sArr, xlFilterValues Application.ScreenUpdating = True ' Inform. MsgBox "Table filtered.", vbInformation End Sub