【VBA】Check if folder exists

You can check if folder exists!

PR

VBA Code

As an example, check if “hogehoge” exists.

hogehoge folder
hogehoge folder
Option Explicit

Sub sampleProc()
    
    Dim fso As Object
    Dim folderPath As String
    
    'Set "Folder path you want to check"
    folderPath = "C:\Users\user\Desktop\hogehoge"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'Check if folder exists
    If fso.FolderExists(folderPath) Then
        MsgBox "The folder is exists."
    Else
        MsgBox "The folder is not exists."
    End If
    
    Set fso = Nothing
    
End Sub

Set “Folder path you want to check”(Line 9).

Check if folder exists(Line 14~18).

PR

Result

You checked if folder exists!

Result
Result
PR

FYI No.1

Also, you can check if file exists!

For more information, see the following article:

PR

FYI No.2

For more information about following, see the following official online manual:

FolderExists method” of “FileSystemObject”

タイトルとURLをコピーしました