ray88’s diary

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

VBA フォルダの存在確認(FileSystemObject)

■呼び出し先プロシージャ

Sub test()
    Dim folderPath As String
    Dim Result As Boolean
    folderPath = "C:\Users\デスクトップ\テスト\テストフォルダ"
    If IsExistsDir(folderPath) Then
        MsgBox "フォルダは存在します"
    Else
        MsgBox "フォルダは存在しません"
    End If
End Sub

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

Function IsExistsDir(targetPath As String) As Boolean
'----------------------------------------------------
'機能:指定されたフォルダパスが存在するか確認し
'   結果をTrueまたはFalseで返す
'引数1:フォルダパス
'-----------------------------------------------------
     With CreateObject("Scripting.FileSystemObject")
        If .FolderExists(targetPath) Then
            IsExistsDir = True
        Else
            IsExistsDir = False
        End If
    End With
End Function

ray88.hatenablog.com