萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 辦公室軟體 >  >> WPS Office >> WPS 綜合教學

WPS 表格快速隱藏非使用區域的 VBA 方案

僅顯示指定區域,其餘隱藏,手動操作繁瑣。透過 VBA 巨集可一次性隱藏選取區域外的所有列與行。

實作步驟

  1. 開啟 WPS 表格 2009,新建空白文件,輸入樣本資料。

  2. 點選「工具」→「巨集」→「建立」新巨集。
  3. 在 Visual Basic 編輯器輸入以下程式碼:
Sub HiddenSurroundRange()
    Dim CelFirst As Range, CelLast As Range
    If Not Selection Is Nothing Then
        With Selection
            Set CelFirst = .Cells(1)
            Set CelLast = .Cells(.Cells.Count)
        End With
        If CelFirst.Address <> "$A$1" Then
            With Range([a1], CelFirst.Offset(IIf(CelFirst.Row = 1, 0, -1), IIf(CelFirst.Column = 1, 0, -1)))
                If CelFirst.Row <> 1 Then .EntireRow.Hidden = True
                If CelFirst.Column <> 1 Then .EntireColumn.Hidden = True
            End With
        End If
        If CelLast.Address <> "$IV$65536" Then
            With Range(CelLast.Offset(IIf(CelLast.Row = 65536, 0, 1), IIf(CelLast.Column = 256, 0, 1)), [IV65536])
                If CelLast.Row <> 65536 Then .EntireRow.Hidden = True
                If CelLast.Column <> 256 Then .EntireColumn.Hidden = True
            End With
        End If
    End If
End Sub
  1. 開啟「檢視」→「工具列」→「控制項工具箱」,插入一個按鈕,指定上述巨集。
  2. 選取欲顯示的使用區域,點擊按鈕,非使用區域即全數隱藏。

提示

WPS 預設未安裝 VBA 環境,需額外安裝。WPS Office 線上文件公測版可共享含巨集文件。開啟含巨集文件時請啟用巨集。

copyright © 萬盛學電腦網 all rights reserved