从数组中特定列的过滤范围中捕获可见值

时间:2019-05-16 11:15:48

标签: arrays excel vba

说明
我有一张工作表,平均约30列,平均行数超过50万(有多个文件)。工作表的第8列包含文件名。我在此列上应用了过滤器,以仅显示带有我要查看的文件名的行

目标
应用过滤器后,我想将第8列中的所有可见行捕获到一个数组中。这就是我在努力挣扎的

代码

Sub GetFilteredColumn()

    Dim oWS As Worksheet: Set oWS = ThisWorkbook.Worksheets("Sheet1")
    Dim iLRow As Long, iRow As Long
    Dim aFilTags As Variant
    Dim oKey As Variant
    Dim oDic As New Dictionary

    With oWS

        ' Get row count of visible rows
        iLRow = .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count

        ' Check if any rows were returned after the filter
        If iLRow > 1 Then
            ' Get column 8 of the filtered range into an array
            ' ** THIS is where i'm trying to capture column 8 into and array **
            'aFilTags = .AutoFilter.Range
            'aFilTags = .AutoFilter.Range.Columns(8).SpecialCells(xlCellTypeVisible).Rows
            aFilTags = .Columns(8).SpecialCells(xlCellTypeVisible)

            ' Get unique values in dictionary
            For iRow = 2 To UBound(aFilTags)
                If Not oDic.Exists(aFilTags(iRow, 1)) Then
                    oDic.Add aFilTags(iRow, 1), aFilTags(iRow, 1)
                End If
            Next

            ' Display the unique list
            iRow = 0
            For Each oKey In oDic.Keys
                iRow = iRow + 1
                .Range("AZ" & iRow).Value = oDic(oKey)
            Next

        End If

    End With

End Sub

很遗憾,由于工作簿中的敏感数据,我无法共享工作表,但很高兴回答任何问题。谢谢你们

1 个答案:

答案 0 :(得分:0)

尝试

Set aFilTags = .AutoFilter.Range.Columns(8).SpecialCells(xlCellTypeVisible)
For Each cl In aFilTags.SpecialCells(xlCellTypeVisible)
            cl = cl & ""
            If Not oDic.Exists(cl) Then
                oDic.Add cl, cl
            End If
Next cl

祝你好运