0
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?

Excelのシート名の一括取得

Posted at

はじめに

Excelで
・シート名の一覧が欲しい
・一覧からシートを作りたい
と思ったことはありませんか。

シート名の一覧を取得

VBEのイミディエイトウィンドウで

For Each sheet In ThisWorkbook.Sheets: debug.print sheet.name: Next

を実行するとシート名の一覧が出力されます。

VBEを開いたり、vbaを実行する手間がかかりますので、アドインにしてみます。
出力先をどうするか悩みましたが、クリップボードにしました。

シート名一覧をクリップボードにコピー
Sub シート名コピー()
    Dim clipboard As Object
    Dim sheet As Worksheet
    Dim sheetName As String
    
    'MSForms.DataObjectを生成
    Set clipboard = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    sheetName = ""

    For Each sheet In ThisWorkbook.Sheets
        'シート名を取得(末尾に改行を入れる)
        sheetName = sheetName & sheet.Name & vbCrLf
    Next i
    
    ' コピーするテキストを設定
    clipboard.SetText sheetName
    
    ' クリップボードにコピー
    clipboard.PutInClipboard
    
    'MSForms.DataObjectを破棄
    Set clipboard = Nothing
End Sub

一覧からシートを作成

選択範囲からシートを生成
Sub シート生成()
    Dim addSheetList As Range
    Dim i As Integer
    Dim j As Integer
    Dim ws As Worksheet
    Dim flag As Boolean
    
    Set addSheetList = Selection
    
    For i = 1 To addSheetList.Rows.Count
        For j = 1 To addSheetList.Columns.Count
            If addSheetList.Cells(i, j).Value <> "" Then
                '同名のシートがないかチェック
                flag = False
                For Each ws In Worksheets
                    If ws.Name = addSheetList.Cells(i, j).Value Then
                        flag = True
                        Exit For
                    End If
                Next ws
                
                If flag = False Then
                    '同名のシートがない場合はシートを追加
                    Worksheets.Add After:=Sheets(Worksheets.Count)
                    'テンプレートシートをコピーする場合
                    'Sheets("テンプレート").Copy After:=Sheets(Worksheets.Count)
                    
                    ActiveSheet.Name = addSheetList.Cells(i, j).Value
                End If
            End If
        Next j
    Next i
    
    Set addSheetList = Nothing
End Sub

おわりに

ちょっとした作業でも自動化すると意外に時間の短縮に繋がります。
VBAはOfficeさえ入っていれば直ぐに使えるので、ちょっとしたものを作るのには最適だと思います。

0
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
0
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?