VBAでApplicationオブジェクトのFileDialogを使用することで、フォルダの選択ダイアログボックスを
取得することができます。
Application.FileDialog プロパティ (Excel)
Application.FileDialogでフォルダのパスを取得するコードは以下になります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Function GetFolderPath() As String '################################################################################### 'フォルダ選択ダイアログを開き、選択したフォルダの絶対パスの末尾に '\を付加してリターンする '----------------------------------------------------------------------------------- '戻り値:選択したフォルダの絶対パスの末尾に\を付加したもの '################################################################################### With Application.FileDialog(msoFileDialogFolderPicker) If .Show Then GetFolderPath = .SelectedItems(1) & "\" End If End With End Function |
GetFolderPath関数に対してテストを行ってみます。
1 2 3 4 5 6 7 |
Sub TestGetFolderPath() Dim folderPath As String folderPath = GetFolderPath MsgBox folderPath End Sub |
フォルダ選択画面が表示されるので、C:\GetFolderPathの中にあるfolderを選択してみます。
選択したフォルダのパスがメッセージボックスで表示されます。
コメント