Range = Range.Value传递可见单元格的方法

我知道复制和粘贴可见单元格的方法(如下),但是,我希望使用范围= range.SpecialCells(xlVisible)相同的方法。 这可能吗?

Sub tstsa() Dim lastrow As Long With Sheets2 lastrow = .Range("Q1048576").End(xlUp).Row .Range("A1:Q" & lastrow).Cells.SpecialCells(xlVisible).Copy End With Sheet8.Range("a1").PasteSpecial xlPasteValues End Sub 

Range.SpecialCells( xlCellTypeVisible )中的集合是Range.Areas的集合。 这与您接触到一个不连续的单元的情况类似。

 Sub tstsa() Dim rws As Long With Sheet2 With .Cells(1, 1).CurrentRegion.Cells With .Resize(.Rows.Count, 17) Debug.Print .SpecialCells(xlCellTypeVisible).Address(0, 0) Sheet8.Range("a1:q1") = .Rows(1).Cells.Value2 For rws = 2 To .SpecialCells(xlVisible).Areas.Count Sheet8.Cells(Rows.Count, 1).End(xlUp)(2).Resize(1, 17) = _ .SpecialCells(xlCellTypeVisible).Areas(rws).Cells.Value2 Next rws End With End With End With End Sub 

在你的情况下,区域将是可见的过滤数据行(包括标题)。 您需要循环遍历区域,并将Range.Value或Range.Value2属性传递给Sheet8以进行直接值传递。

如果行是连续的,则Area可以由多行数据组成。 我已经离开了一个Debug.Print ,所以你可以在VBE的立即窗口中观察Areas集合的地址。