0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【実務VBA④】現場で使えるVBA便利ツール5選

0
Last updated at Posted at 2026-04-02

はじめに

日々のExcel業務で、こんな作業に時間を取られていませんか?

  • ファイル一覧を手作業で作る
  • CSVを1つずつ開く
  • 同じ処理を複数ファイルに繰り返す

こうした作業は、VBAで自動化できます。
今回は、現場でよく使うVBAツールを5つ厳選して紹介します。
必要なところだけ読めばOK/すべてコピペで使えます。

目次

  1. ファイル一覧取得ツール
  2. CSV一括取り込みツール
  3. シート一括コピーツール
  4. Excel一括処理ツール
  5. レポート自動作成ツール

① ファイル一覧取得

使用シーン

  • フォルダ内のファイルを一覧化したい
  • ファイル管理・棚卸し

処理内容

  • 指定フォルダ内のファイル名を取得
  • Excelに一覧出力

コード(基本版)

Sub GetFileList()

    Dim folderPath As String   ' フォルダパス
    Dim fileName As String     ' ファイル名
    Dim rowNum As Long         ' 出力行

    ' フォルダ選択ダイアログを表示
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = -1 Then
            folderPath = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With

    rowNum = 1

    ' フォルダ内のファイルを取得
    fileName = Dir(folderPath & "*.*")

    Do While fileName <> ""
        Cells(rowNum, 1).Value = fileName   ' ファイル名を出力
        rowNum = rowNum + 1
        fileName = Dir                      ' 次のファイルへ
    Loop

    MsgBox "完了しました!"

End Sub

応用: サブフォルダも含めてすべてのファイルを取得したい場合

Sub GetFileList_Recursive()

    Dim folderPath As String
    Dim rowNum As Long

    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = -1 Then
            folderPath = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With

    rowNum = 1

    ' 再帰処理開始
    Call GetFiles(folderPath, rowNum)

    MsgBox "完了しました!"

End Sub

Sub GetFiles(ByVal folderPath As String, ByRef rowNum As Long)

    Dim fileName As String
    Dim subFolder As String

    ' ファイル取得
    fileName = Dir(folderPath & "\*.*")

    Do While fileName <> ""
        If fileName <> "." And fileName <> ".." Then
            ' フォルダでなければ出力
            If (GetAttr(folderPath & "\" & fileName) And vbDirectory) = 0 Then
                Cells(rowNum, 1).Value = folderPath & "\" & fileName
                rowNum = rowNum + 1
            End If
        End If
        fileName = Dir
    Loop

    ' サブフォルダ取得
    subFolder = Dir(folderPath & "\*", vbDirectory)

    Do While subFolder <> ""
        If subFolder <> "." And subFolder <> ".." Then
            ' フォルダの場合は再帰処理
            If (GetAttr(folderPath & "\" & subFolder) And vbDirectory) <> 0 Then
                Call GetFiles(folderPath & "\" & subFolder, rowNum)
            End If
        End If
        subFolder = Dir
    Loop

End Sub

② CSV一括取り込み

使用シーン

  • 毎日CSVデータを取り込む
  • 複数ファイルの集計

処理内容

  • フォルダ内のCSVを全件取得
  • ファイルごとにシート作成して読み込み
Sub ImportCSVFiles()

    Dim folderPath As String   ' CSVフォルダ
    Dim fileName As String     ' ファイル名
    Dim ws As Worksheet        ' 出力シート

    folderPath = InputBox("CSVフォルダのパスを入力してください")

    fileName = Dir(folderPath & "\*.csv")

    Do While fileName <> ""
        
        ' 新規シート作成
        Set ws = Worksheets.Add
        ws.Name = Replace(fileName, ".csv", "")

        ' CSV読み込み
        With ws.QueryTables.Add( _
            Connection:="TEXT;" & folderPath & "\" & fileName, _
            Destination:=ws.Range("A1"))
            
            .TextFileCommaDelimiter = True  ' カンマ区切り
            .Refresh                        ' 読み込み実行
        End With

        fileName = Dir
    Loop

    MsgBox "CSV取込完了!"

End Sub

③ シート一括コピー

使用シーン

  • 月別・日別シート作成
  • テンプレート展開

処理内容

  • テンプレートシートを複製
  • 指定回数分シートを作成
Sub CopySheetMultiple()

    Dim i As Integer
    Dim baseSheet As Worksheet

    ' テンプレートシート
    Set baseSheet = Worksheets("Template")

    For i = 1 To 12
        baseSheet.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Sheet_" & i   ' 名前変更
    Next i

    MsgBox "シート作成完了!"

End Sub

④ Excel一括処理

使用シーン

  • 複数Excelに同じ処理を適用
  • データ修正・整形

処理内容

  • フォルダ内のExcelを順に開く
  • 指定処理を実行して保存
Sub ProcessAllExcelFiles()

    Dim folderPath As String   ' 対象フォルダ
    Dim fileName As String     ' ファイル名
    Dim wb As Workbook         ' 対象ブック

    folderPath = InputBox("フォルダパスを入力してください")

    fileName = Dir(folderPath & "\*.xlsx")

    Application.ScreenUpdating = False  ' 画面更新停止(高速化)

    Do While fileName <> ""

        ' ファイルを開く
        Set wb = Workbooks.Open(folderPath & "\" & fileName)

        ' ★ここに処理を書く(例)
        wb.Sheets(1).Range("A1").Value = "処理済み"

        ' 保存して閉じる
        wb.Close SaveChanges:=True

        fileName = Dir
    Loop

    Application.ScreenUpdating = True

    MsgBox "一括処理完了!"

End Sub

⑤ レポート自動作成

使用シーン

  • 集計レポート作成
  • 上司提出資料

処理内容

  • データを集計
  • 新規シートに結果出力
Sub CreateReport()

    Dim ws As Worksheet
    Dim result As Long

    ' データシート
    Set ws = Worksheets("データ")

    ' A列の合計を計算
    result = Application.WorksheetFunction.Sum(ws.Range("A:A"))

    ' レポートシート作成
    Worksheets.Add.Name = "レポート"

    Range("A1").Value = "合計"
    Range("B1").Value = result

    MsgBox "レポート作成完了!"

End Sub

まとめ

今回紹介したツールは、すべて
現場でそのまま使えるレベルの実用コードです。
特におすすめ

  • ファイル一覧取得
  • CSV一括取り込み
  • Excel一括処理

この3つだけで、作業時間は大きく削減できます。

実務VBAシリーズ

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?