簡體   English   中英

Excel VBA-設置范圍過濾表可見單元格

[英]Excel VBA - Set Range Filtered table Visible Cells

我正在使用此代碼將一系列過濾表設置為僅可見單元格。

Set rngMyRange = Selection.SpecialCells(xlCellTypeVisible)

但是,如果只選擇一個單元格,那就是一個奇怪的錯誤,即代碼選擇了過濾表中所有使用的范圍。

如果選擇大於一個單元格,則代碼將按預期工作。 有沒有解決的辦法?

通常使用“選擇”不是一個好習慣; 我猜您只需要獲取可見單元格的范圍,您就可以輕松地使用它:

Sub GetVisibleRangeOnly()
    Dim tbl As ListObject
    Dim rng As Range

    'change the name of the table and worksheet as you need
    Set tbl = Worksheets("Sheet1").ListObjects("Table1")

    'Note: if there is no visible cell after filtraton rng IS NOTHING will be TRUE
    Set rng = tbl.DataBodyRange.SpecialCells(xlCellTypeVisible)

End Sub

暫無
暫無

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

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