【VBA】MkDirでフォルダを作る
記事更新日:2022-11-04

MkDir パス

Make Directory という意味
フォルダのことを「ディレクトリ」とも呼ぶ
MkDir でフォルダ作成
フォルダ作成後、フォルダのパスを返す

引数 path の中身は、ThisWorkbook.Path など
Public Function MakeFolderPath( _
ByVal path As Variant, _
ByVal folderName As String) As Variant
folderPath = path & "¥" & folderName
Dim folderPath As Variant
'フォルダが存在していない場合のみ、フォルダ作成
If NotExistsFolder(folderPath) Then MkDir folderPath
'フォルダ作成してないときは中身が空になる
MakeFolderPath = folderPath
End Function
'フォルダがすでに存在していないことを確認
Private Function NotExistsFolder(ByVal myPath As String) As Boolean
NotExistsFolder = False
If Dir(myPath, vbDirectory) = "" Then NotExistsFolder = True
End Function

パスを返す目的は、メッセージ表示に使ったりとか、フォルダを開いたりとか
関連記事
2022-11-04
編集後記:
この記事の内容がベストではないかもしれません。