【VBA】MkDirでフォルダを作る
作成日:2022-11-04
更新日:2022-12-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
編集後記:
この記事の内容がベストではないかもしれません。
記事一覧
-
[VBA]close booksave or not 【VBA】ブックを閉じる3つの方法|保存して/保存しないで/ブック名で/ -
日付を文字列に変えたい 【VBA】日付型を文字列にするCStr と format -
[VBA]Horizontal Paste1D array 【VBA】一次元配列を横方向にシートに貼り付け【範囲をリサイズする】 -
dictionaryをセットする 【VBA】Dictionaryのセット|CreateObject(“Scripting.Dictionary”) -
vbaの色設定 【VBA】色の定数は8つ|RGB対称表 -
[VBA]InStr文字列確認 【VBA】指定の文字列が含まれているか|InStr