ray88’s diary

お仕事で困ったとき用の自分用の覚書

VBA 指定したパスのフォルダ内にあるファイルの総数を返す

■フォルダ内のファイル件数を確認する
 呼び出し元プロシージャ

Sub subFileCount()
    Dim strPath As String
    Dim intFilesCount As Integer    
    strPath = "C:\Users\デスクトップ\テスト"    
    intFilesCount = getFileCount(strPath)
    MsgBox intFilesCount
End Sub

 ファンクションプロシージャ

Function getFileCount(strParentFolderPath As String) As Integer
'-------------------------------------------------------------
'機能:指定したパスのフォルダに存在するファイルの総数を返す
'引数1:ファイル数を確認したい対象の親フォルダのパス
'-------------------------------------------------------------
Dim FSO As Object
Dim objFiles As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objFiles = FSO.GetFolder(strParentFolderPath).Files
    getFileCount = objFiles.Count
End Function