ray88’s diary

お仕事で困ったとき用の自分用の覚書

Excel VBA ダイアログを表示してフォルダを選択する

■ ダイアログを表示してフォルダを選択する部品

Function fncGetFolderPath() As String
'----------------------------------------------------------------
'機能:フォルダ選択ダイアログを表示し、選択したフォルダパスを返す
'戻り値:フォルダパス
'----------------------------------------------------------------
    Dim objDialog As Object   'FileDialogオブジェクト格納用
    'FileDialogオブジェクトをインスタンス化
    Set objDialog = Application.FileDialog(msoFileDialogFolderPicker)
    'ダイアログを開いて選択したフォルダを親フォルダパスに指定
    If objDialog.Show Then
        fncGetFolderPath = objDialog.SelectedItems(1)
    Else
        fncGetFolderPath = ""
    End If
End Function