フォルダ 指定フォルダ指定拡張子のファイル一覧を変数で返す

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。

Option Explicit


Sub FileVariableInFolder(ByRef strFile() As String _
ByVal strFolderPath As StringByVal strExtension As String)
'**************************************************
'指定フォルダ指定拡張子のファイル一覧を変数で返す
'**************************************************

Dim buf As String, i As Long
    i = 0
    buf = Dir(strFolderPath & "\*." & strExtension)
    Do While buf <> ""
        ReDim Preserve strFile(i) As String
        strFile(i) = buf
        i = i + 1
        buf = Dir()
    Loop

End Sub


Private Sub test()
Dim strFile() As String
Dim strFolderPath As String
Dim strExtension As String

strFolderPath = ThisWorkbook.Path & "\xxx\xxxxx\photo"
strExtension = "jpg"
Call FileVariableInFolder(strFile, strFolderPath, strExtension)

MsgBox "最初のファイル名は:" & strFile(LBound(strFile))
MsgBox "最後のファイル名は:" & strFile(UBound(strFile))
MsgBox "合計数:" & UBound(strFile) + 1

End Sub

 

2000年01月01日|[VBサンプルコード]:[フォルダ]