由于 B 列将受到与 A 列相同的过滤器,这里有一个灵活的解决方案,允许您指定要使用的列:
Sub FilterColumn(ColumnNumber As Long)
Dim LastRow As Long
Dim rng As Range
Dim rngVisible As Range
Dim cell As Range
Dim Array1() As Variant
Dim i As Long
With ActiveSheet
Set rng = .Columns(ColumnNumber)
LastRow = .Cells(.Rows.Count, ColumnNumber).End(xlUp).Row
On Error Resume Next
Set rngVisible = .Range(.Cells(2, ColumnNumber), .Cells(LastRow, ColumnNumber)).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rngVisible Is Nothing Then
ReDim Preserve Array1(1 To rngVisible.Cells.Count)
i = 1
For Each cell In rngVisible
Array1(i) = cell.Value
i = i + 1
Next cell
End If
End With
End Sub
B列这样称呼它:
FilterColumn 2
作为旁注,我建议您不要将 Excel 保留字用于变量名。范围是保留字。