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?

More than 5 years have passed since last update.

EXcel VBAで全てのExcelファイル(サブフォルダを含む)を探して処理する

Last updated at Posted at 2020-02-14

概要

指定されたフォルダの中にある全てのExcelファイル(サブフォルダ内も含む)を処理するコード

参照設定

FileSystemObject、Folderオブジェクト、Fileオブジェクトを使うので参照設定。

  • コードエディタ
  • →「ツール」
  • →「参照設定」
  • →「Microsoft Scripting Runtime」にチェック
  • →「OK」

コード


Sub main()
    Dim targetFolder As String
    targetFolder = "C:\hoge"
    
    SearchFolder targetFolder
End Sub

Sub SearchFolder(ByVal path As String)
    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject
    
    Dim tFol As Folder
    Set tFol = FSO.GetFolder(path)
    
    '全てのフォルダを探索する
    Dim fol As Folder
    For Each fol In tFol.SubFolders
        SearchFolder fol.path
    Next
    
    '全てのファイルをチェックする
    Dim fi As File
    For Each fi In tFol.Files
        '一時ファイルは除外
        If Left$(fi.Name, 2) <> "~$" Then
            'Excelファイルのみ
            If Right$(fi.Name, 5) = ".xlsx" Then
                ProcFile fi.path
            End If
        End If
    Next
End Sub

Sub ProcFile(ByVal path As String)
    'ファイルごとの処理
End Sub

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?