呼び出し元プロシージャ
Sub TestGetArray() Dim arrayFiles() As String Dim strFolderPath As String Dim i As Integer Dim intExcelRow As Integer strFolderPath = Sheets("Sheet1").Range("C2") arrayFiles = getFileArray(strFolderPath) intExcelRow = 5 For i = 0 To UBound(arrayFiles) Debug.Print arrayFiles(i) Next End Sub
Functionプロシージャ
Function getFileArray(strFolderPath As String) As String() '---------------------------------------------------------- '機能 :指定したフォルダパスのフォルダ内のファイルを取得する '引数1 :対象フォルダパス '戻り値 :取得したファイルのパスを配列で返す '---------------------------------------------------------- Dim FSO As Object Dim tmpFile As Object Dim objFiles As Object Dim intFilesCount Dim arrayFiles() As String Dim i As Integer Set FSO = CreateObject("Scripting.FileSystemObject") Set objFiles = FSO.GetFolder(strFolderPath).Files intFilesCount = objFiles.Count i = 0 ReDim arrayFiles(intFilesCount - 1) With FSO.GetFolder(strFolderPath) For Each tmpFile In .Files arrayFiles(i) = tmpFile.Path i = i + 1 Next End With getFileArray = arrayFiles() End Function