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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With