Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA EXCEL To Prompt User Response to Select Folder and Return the Path as String Variable [duplicate]

Tags:

excel

vba

I am trying to write a VBA code where a dialog box would appear for the user to select where they want to save the files. However, I just need the path value (e.g. c:\Desktop\Values) returned as a string variable so that I could use it in another function. Any help would be appreciated.

like image 399
Eric Avatar asked Oct 15 '14 21:10

Eric


People also ask

How do I browse a folder in Excel VBA?

Use msoFileDialogFolderPicker to browse for and select a folder with VBA. The msoFileDialogFolderPicker is part of the Application. FileDialog family in Excel. The msoFileDialogFolderPicker is just one of many Application.

How do I use FileDialog in Excel VBA?

Excel VBA FileDialog – Example #1Step 1: Go to the Developers tab and click on Visual Basic. Step 2: Open a Module from the Insert menu option as shown below. Step 3: Start the subprocedure to start working on example. Step 4: Declare a variable as Filedialog as shown below.


1 Answers

Consider:

Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function

and as jkf points out, from Mr Excel

like image 138
Gary's Student Avatar answered Oct 12 '22 11:10

Gary's Student