銀河鉄道

【VBA】オートフィルタ解除&非表示を表示する|.FilterMode/.Outline

サムネイル
[VBA]Reset filtersand outlines

オートフィルタを解除し、行列を再表示する

シートの可視状態を初期化する

' Reset all filters, outlines, and hidden rows/columns|全フィルタ・グループ・非表示行列を解除
Public Function ResetFilterMode(ByVal ws As Worksheet)
	On Error GoTo ErrHdl

	With ws
		' オートフィルタ解除
		If .FilterMode Then .ShowAllData

		' グループ展開(最大階層まで)
		With .Outline
			.ShowLevels ColumnLevels:=8, RowLevels:=8
		End With

		' 非表示解除
		.Cells.EntireColumn.Hidden = False
		.Cells.EntireRow.Hidden = False
	End With

	Exit Function

ErrHdl:
	Call MsgError("ResetFilterMode") ' エラー発生時の識別用に関数名を渡すと良い
End Function

オートフィルタ解除 → グループ展開 → 非表示行列の表示

フィルタもグループもリセット、一発で見える化

Reset filters and outlines — reveal the full worksheet in one shot.

フィルタ解除|Remove AutoFilter

If ws.FilterMode Then ws.ShowAllData
  • .FilterModeオートフィルタや詳細設定フィルタが有効なときに True
  • .ShowAllData を呼ぶとすべてのデータを再表示(フィルタ解除)する。
  • エラー回避のため、この条件付きで呼ぶのがベストプラクティス。

グループ展開|Expand Outline Groups

With ws.Outline
    .ShowLevels ColumnLevels:=5, RowLevels:=5
End With
  • .Outline は行・列のグループ化(アウトライン)機能にアクセスするプロパティ。
  • .ShowLevels で「第 n 階層まで展開」できる。
  • ColumnLevels:=5 としているのは「最大5階層あるかもしれない」という想定。
     → 実際は ColumnLevels:=8, RowLevels:=8 のように十分大きくしてもOK。

非表示行列を表示|Unhide All Rows and Columns

With ws.Cells
    .EntireColumn.Hidden = False
    .EntireRow.Hidden = False
End With
  • シート上のすべての列・行を対象に、HiddenFalse に戻している。
  • .Cells に対して EntireColumnEntireRow を指定することで全体を対象にできる。

Referenced Insights & Citations

| AutoFilter|オートフィルタ |
| Outline|アウトライン |
| ShowLevels|階層展開 |
| Hidden Property|非表示プロパティ |
| Error Handling|エラーハンドリング |

Always reset outlines and filters safely—visibility bugs are the silent killers of Excel macros.

データをすべて削除したい場合は、
フィルタをリセットしたうえでおこなう

著者

author
月うさぎ

編集後記:
この記事の内容がベストではないかもしれません。

記事一覧