0

此代码将通过过滤范围并仅将可见单元格插入到数组中(假设 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

4

3 回答 3

1
For Each cell In Range.SpecialCells(xlCellTypeVisible)
    Array1(i) = cell.Offset(ColumnOffset:=1).Value
    i = i + 1
Next c
于 2012-08-17T17:44:14.307 回答
1
For Each cell In Range.SpecialCells(xlCellTypeVisible)
   Array1(i) = cell.Offset(0, 1).Value
   i = i + 1

Next c
于 2012-08-17T17:46:31.290 回答
1

由于 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 保留字用于变量名。范围是保留字。

于 2012-08-17T18:15:18.477 回答