问题
Description:
I have a sheet with about 30 columns and more than 500k rows on average (there are multiple files). Column 8 of the sheet holds filenames. I am applying filter on this column to only display rows with filenames that I want to see
Goal:
After applying the filter, I want to capture all visible rows in column 8 into an array. This is the bit I'm struggling with
Code:
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
Unfortunately I am not able to share the worksheet because of sensitive data in the workbook but happy to answer any questions. Thanks guys
回答1:
try with this
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
good luck
来源:https://stackoverflow.com/questions/56167270/capture-visible-values-from-filtered-range-from-a-particular-column-in-an-array