如何访问Excel中过滤范围之外的单元格内容?

此代码将经过一个过滤的范围,并只插入可见单元格到一个数组(假装列A根据我的标准过滤)。 但是,我真正想要做的是移动一列,并将“B3”的内容插入我的数组而不是“A3”。 我如何修改我的代码?

For Each cell In Range.SpecialCells(xlCellTypeVisible) Array1(i) = cell.Value i = i + 1 Next c 

我在想像Array1(i)= Cells(cell.Row,cell.Column + 1).Value

 For Each cell In Range.SpecialCells(xlCellTypeVisible) Array1(i) = cell.Offset(ColumnOffset:=1).Value i = i + 1 Next c 
 For Each cell In Range.SpecialCells(xlCellTypeVisible) Array1(i) = cell.Offset(0, 1).Value i = i + 1 Next c 

由于B列将会受到与A列相同的filter的限制,下面是一个灵活的解决scheme,允许您指定要使用的列:

 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 

作为一个侧面说明,我build议你不要使用Excel保留字的variables名称。 范围是保留字。