![](https://excel-vba.work/wp-content/themes/cocoon-master/images/b-woman.png)
VBAで
・空フォルダかどうかを判定
できます!
![](https://excel-vba.work/wp-content/themes/cocoon-master/images/b-woman.png)
Windows APIによって判定
できます!
![](https://excel-vba.work/wp-content/themes/cocoon-master/images/b-woman.png)
Windows APIの1つである
・PathIsDirectoryEmpty関数
を使用します!
VBAコード
ここでは例として
・デスクトップ配下のフォルダ「test」が
・空フォルダかどうかを判定
します。
![フォルダ「test」は空フォルダ](https://excel-vba.work/wp-content/uploads/2022/12/vba_folder_empty_001.png)
Option Explicit
'WindowsAPI PathIsDirectoryEmpty
Declare PtrSafe Function PathIsDirectoryEmpty Lib "SHLWAPI.DLL" Alias "PathIsDirectoryEmptyA" (ByVal pszPath As String) As Boolean
Sub sample()
Dim targetFolder As String
'フォルダを指定
targetFolder = "C:\Users\user\Desktop\test"
'空フォルダかどうかを判定
If PathIsDirectoryEmpty(targetFolder) = 1 Then
MsgBox "空フォルダです!"
Else
MsgBox "空フォルダではありません!"
End If
End Sub
実行結果
空フォルダかどうかを判定できました。
![実行結果](https://excel-vba.work/wp-content/uploads/2022/12/vba_folder_empty_002.png)
参考
上記のVBAコードで使用した以下の詳細は、公式サイトをご確認ください。
●Windows APIの「PathIsDirectoryEmpty関数」