簡體   English   中英

范圍= Range.Value方法以傳輸可見單元格

[英]Range = Range.Value method to transfer visible cells

我知道復制和粘貼可見單元格的方法(如下所示),但是我希望使用range = 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

在您的情況下,“區域”將是過濾數據的可見行(包括標題)。 您需要遍歷Areas,並將Range.ValueRange.Value2屬性帶到Sheet8進行直接值傳輸。

如果行是連續的,則一個區域可以包含多於一行的數據。 我留了一個Debug.Print ,以便您可以在VBE的“即時”窗口中觀察Areas集合的地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM