1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

シートが増えた月次ファイルをさばく、非表示・グループ化・一覧化の3つのマクロ

1
Last updated at Posted at 2026-09-18

シートが増えた月次ファイルをさばく、非表示・グループ化・一覧化の3つのマクロ

タグ: Excel VBA 業務効率化 Tips 初心者
投稿先: Qiita


はじめに

12か月分の月別シート、部署別シート、集計用の補助シート——ブックのシート数が増えるほど、「いま必要なシートだけ見たい」「全シートに同じ見出しを入れたい」「どんなシートがあるか把握したい」という場面が増えます。この記事では、そんなシート操作を数十秒で終わらせる3つのマクロを、使う場面とあわせて紹介します。

1. 補助シートを隠して、見せたいシートだけにする

HideSheetは、指定したシートを非表示にするマクロです。計算用の補助シートや、集計の元データを置いたシートを隠しておけば、ブックを配るときに見せたいシートだけが並びます。表示中のシートが1枚になる操作は止めてくれるので、ブックが開けなくなる心配なく使えます。

Sub HideSheet()
    Dim ws As Worksheet
    Dim sheetName As String
    Dim visibleCount As Long
    Dim i As Long

    On Error GoTo ErrorHandler

    visibleCount = 0
    For i = 1 To Worksheets.Count
        If Worksheets(i).Visible = True Then
            visibleCount = visibleCount + 1
        End If
    Next i

    If visibleCount <= 1 Then
        MsgBox "最後の表示中のシートは非表示にできません。", vbExclamation
        Exit Sub
    End If

    sheetName = InputBox("非表示にするシート名を入力:", "シート非表示", ActiveSheet.Name)

    If sheetName = "" Then
        Exit Sub
    End If

    On Error Resume Next
    Set ws = Worksheets(sheetName)
    On Error GoTo ErrorHandler

    If ws Is Nothing Then
        MsgBox "「" & sheetName & "」は存在しません。", vbExclamation
        Exit Sub
    End If

    If ws.Visible = xlSheetVisible Then
        visibleCount = 0
        For i = 1 To Worksheets.Count
            If Worksheets(i).Visible = True And i <> ws.Index Then
                visibleCount = visibleCount + 1
            End If
        Next i
        If visibleCount = 0 Then
            MsgBox "このシートを非表示にすると表示中のシートがなくなります。", vbExclamation
            Exit Sub
        End If
        ws.Visible = xlSheetHidden
        MsgBox "「" & sheetName & "」を非表示にしました。", vbInformation
    Else
        MsgBox "「" & sheetName & "」は既に非表示です。", vbInformation
    End If
    Exit Sub

ErrorHandler:
    MsgBox "エラーが発生しました: " & Err.Description, vbCritical
End Sub

使う場面: 月次報告のブックを部長に渡す前に、「集計元」「計算用」といったシートを隠します。シート名はInputBoxで入力します。一度隠したシートは、シートタブの右クリックから「再表示」で戻せます。

2. 12か月分のシートに、同じ見出しを一度で入れる

GroupSheetsは、シート名をカンマ区切りで入力して、複数のシートをまとめて選択(グループ化)するマクロです。グループ化した状態で入力や書式設定をすると、選んだすべてのシートに同時に反映されます。

Sub GroupSheets()
    Dim ws As Worksheet
    Dim sheetNames() As String
    Dim inputList As String
    Dim i As Long
    Dim validCount As Long
    Dim tempWs As Worksheet

    On Error GoTo ErrorHandler

    inputList = "グループ化するシート名をカンマ区切りで入力:" & vbCrLf & vbCrLf
    For i = 1 To Worksheets.Count
        inputList = inputList & "  " & Worksheets(i).Name & vbCrLf
    Next i

    inputList = InputBox(inputList, "シートグループ化", "")

    If inputList = "" Then
        Exit Sub
    End If

    Dim entries() As String
    entries = Split(inputList, ",")

    ReDim sheetNames(1 To UBound(entries) + 1)
    validCount = 0

    For i = LBound(entries) To UBound(entries)
        Dim sheetName As String
        sheetName = Trim(entries(i))
        For Each tempWs In Worksheets
            If tempWs.Name = sheetName Then
                validCount = validCount + 1
                sheetNames(validCount) = sheetName
                Exit For
            End If
        Next tempWs
    Next i

    If validCount = 0 Then
        MsgBox "有効なシート名が指定されませんでした。", vbExclamation
        Exit Sub
    End If

    If validCount = 1 Then
        MsgBox "シートは1つしか選択されていません。グループ化するには2つ以上必要です。", vbExclamation
        Exit Sub
    End If

    Worksheets(sheetNames(1)).Select
    For i = 2 To validCount
        Worksheets(sheetNames(i)).Select False
    Next i

    MsgBox validCount & "枚のシートをグループ化しました。", vbInformation
    Exit Sub

ErrorHandler:
    MsgBox "エラーが発生しました: " & Err.Description, vbCritical
End Sub

使う場面: 1月,2月,3月のように月別シートを指定して実行し、A1に「売上日報」と入力、見出し行を太字にすると、3枚とも同じ見出しになります。作業が終わったら、いずれかのシートタブをクリックしてグループを解除してから保存しておくと、次に開いたときも普通の状態で使えます。

3. シートの一覧を、メッセージとシート上に出す

ListAllSheetsは、ブック内のすべてのシート名を、メッセージボックスと現在のシート上の表に出力するマクロです。

Sub ListAllSheets()
    Dim ws As Worksheet
    Dim outputWs As Worksheet
    Dim sheetList As String
    Dim i As Long
    Dim outputRow As Long
    Dim lastRow As Long
    Dim outputRange As Range

    On Error GoTo ErrorHandler

    sheetList = "シート一覧:" & vbCrLf & vbCrLf

    For Each ws In Worksheets
        sheetList = sheetList & "  " & ws.Index & ": " & ws.Name & vbCrLf
    Next ws

    sheetList = sheetList & vbCrLf & "合計: " & Worksheets.Count & "枚"

    MsgBox sheetList, vbInformation, "シート一覧"

    Set outputWs = ActiveSheet
    lastRow = outputWs.Cells(outputWs.Rows.Count, 1).End(xlUp).Row

    If lastRow < 1 Then lastRow = 1

    outputRow = lastRow + 2
    outputWs.Cells(outputRow, 1).Value = "シート一覧"
    outputWs.Cells(outputRow, 1).Font.Bold = True
    outputWs.Cells(outputRow, 1).Font.Size = 14
    outputRow = outputRow + 1

    outputWs.Cells(outputRow, 1).Value = "番号"
    outputWs.Cells(outputRow, 2).Value = "シート名"
    outputWs.Cells(outputRow, 1).Font.Bold = True
    outputWs.Cells(outputRow, 2).Font.Bold = True
    outputWs.Range(outputWs.Cells(outputRow, 1), outputWs.Cells(outputRow, 2)).Interior.Color = RGB(200, 200, 200)

    outputRow = outputRow + 1

    For i = 1 To Worksheets.Count
        outputWs.Cells(outputRow, 1).Value = i
        outputWs.Cells(outputRow, 2).Value = Worksheets(i).Name
        outputRow = outputRow + 1
    Next i

    outputWs.Columns("A:B").AutoFit
    Exit Sub

ErrorHandler:
    MsgBox "エラーが発生しました: " & Err.Description, vbCritical
End Sub

使う場面: 引き継ぎを受けた、シートが30枚あるブックの全体像をつかみたいときや、目次シートを作る下準備に使えます。番号とシート名の2列で出力されるので、そのままハイパーリンクの元データにもできます。

まとめ

シートが増えたブックは、「隠す」「まとめて編集する」「全体を把握する」の3つができるだけで扱いやすくなります。HideSheetで見せる範囲を整え、GroupSheetsで共通の見出しを一括入力し、ListAllSheetsで全体を確認する——この流れを月次作業に組み込んでみてください。

紹介した3つを含む、VBAマクロを100本、サイトにまとめて公開しています。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?