Private Sub CommandButton1_Click()
CommandButton1.Caption = SelectFolder_FileDialog
End Sub
フォルダ選択ダイアログExcel標準モジュールコード
Public Function SelectFolder_FileDialog()
If Application.FileDialog(msoFileDialogFolderPicker).Show Then
SelectFolder_FileDialog = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
Else
SelectFolder_FileDialog = ""
End If
End Function
フォルダ選択ダイアログExcel標準モジュールコード(初期フォルダを指定する方法)
Public Function SelectFolder_FileDialog(iniDir As String) As String
Dim s1 As String
'フォルダ選択ダイアログ
With Application.FileDialog(msoFileDialogFolderPicker)
'タイトル
.Title = "フォルダを選択してください"
'初期フォルダ
.InitialFileName = iniDir
If .Show = -1 Then
'選択された
s1 = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
If Right$(s1, 1) <> "\" Then s1 = s1 + "\"
SelectFolder_FileDialog = s1
Else
SelectFolder_FileDialog = ""
End If
End With
End Function
フォルダ選択ダイアログの画面
スポンサーリンク
■方法2 Shell
コマンドボタン クリックイベントでフォルダ選択ダイアログを開く
Private Sub CommandButton1_Click()
CommandButton1.Caption = SelectFolder_Shell
End Sub
フォルダ選択ダイアログExcel標準モジュールコード
Public Function SelectFolder_Shell()
Dim Shell, myPath
Set Shell = CreateObject("Shell.Application")
Set myPath = Shell.BrowseForFolder(&O0, "フォルダを選んでください", &H1 + &H10, "C:\")
If Not myPath Is Nothing Then
SelectFolder_Shell = myPath.Items.Item.Path
Else
SelectFolder_Shell = ""
End If
End Function