📊 Excel VBA Top100
📋

GroupSheets

シート管理

複数のシートをグループ化する

🎬 デモGIF準備中

📖 使い方

  1. VBAエディタを開く(Alt + F11)
  2. モジュールを挿入(挿入 > モジュール)
  3. 下記VBAコードをコピー&ペースト
  4. ブック上で実行(Alt + F8 でマクロ選択)
💡 実行例: 実行するとグループ化するシート名を選択するダイアログが表示される

💻 VBAコード

' GroupSheets
' -----------------
' Macro Name: GroupSheets
' Description: 複数のシートをグループ化する
' Parameters: なし
' Returns: なし
' Usage: 実行するとグループ化するシート名を選択するダイアログが表示される
' -----------------

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