【VBA】ファイル/フォルダ存在チェック

ファイル存在チェック
ファイルが存在すればtrue、なければfalseを返します。
'ファイルのパス
file_path = "C:\xxxx\yyyy\zzz.txt"
'ファイル存在チェック
result = Dir(file_path) <> ""
If (result) Then
MsgBox "ファイルが存在しました。"
Else
MsgBox "ファイルが存在しませんでした。"
End If
関数化すると↓の感じです。
'ファイル存在チェック
Public Function IsExistsFile(file_path)
IsExistsFile = Dir(file_path) <> ""
End Function
フォルダ存在チェック
フォルダが存在すればtrue、なければfalseを返します。
'フォルダのパス
folder_path = "C:\xxxx\yyyy"
'フォルダ存在チェック
result = Dir(folder_path, vbDirectory) <> ""
If (result) Then
MsgBox "フォルダが存在しました。"
Else
MsgBox "フォルダが存在しませんでした。"
End If
関数化すると↓の感じです。
'フォルダ存在チェック
Public Function IsExistsFolder(ByVal folder_path)
If (Right(folder_path, 1) <> "\") Then
folder_path = folder_path & "\"
End If
IsExistsFolder = Dir(folder_path, vbDirectory) <> ""
End Function
末尾が\で終わっていないとうまく動かないので、\で終わっていなければ\を付ける処理を追加しています。
以上になります。
お読み頂き、ありがとうございました。