Pass File Path from MsoFileDialogueFolderPicker back to Button Click Event

rkaczano

Board Regular
Joined
Jul 24, 2013
Messages
141
Office Version
  1. 365
Platform
  1. Windows
I have a folder picker sub using the msoFileDialogFolderPicker. I trigger it based on a button click even in a userform. I pass the file path to a caption control within the browseFolderPath routine. Label4.Caption = strFilePath is the line of code. This occurs in the sub itself

I want to have this sub triggered by the click events of multiple other buttons and be able to store the individual file paths to individual caption controls associated with each button. How do I pass the file path that results from each pass of this routine back to the individual button click events?

VBA Code:
Private Sub CommandButton3_Click()
    browseFolderPath
End Sub

VBA Code:
Sub browseFolderPath()
    On Error GoTo err
    Dim fileExplorer As FileDialog
    Dim strFilePath As String
    
    Set fileExplorer = Application.FileDialog(msoFileDialogFolderPicker)

    'To allow or disable to multi select
    fileExplorer.AllowMultiSelect = False

    With fileExplorer
        If .Show = -1 Then 'Any folder is selected
            strFilePath = .SelectedItems.item(1)
        Else ' else dialog is cancelled
            MsgBox "You have cancelled the dialogue"
            strFilePath = "" ' when cancelled set blank as file path.
        End If
   
    Label4.Caption = strFilePath
    End With
err:
    Exit Sub
    
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Change browseFolderPath to a Function which returns a String, the selected folder path or "" if it was cancelled. Delete the Label4.Caption line. Call the function like this in each Click event handler:

VBA Code:
Private Sub CommandButton3_Click()
    Dim folderPath As String
    folderPath = browseFolderPath
    If folderPath <> "" Then
        Label4.Caption = folderPath
    End If
End Sub
 
Upvote 0
Using the suggestions from @John_w your code then becomes:
VBA Code:
Function BrowseFolderPath() As String
    BrowseFolderPath = vbNullString
    On Error GoTo Err_Exit
    '
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False   ' Disable multi select
        If .Show = -1 Then          ' A folder was selected
            BrowseFolderPath = .SelectedItems.Item(1)
        Else ' else dialog is cancelled
            MsgBox "You have cancelled the dialogue"
        End If
    End With
Err_Exit:
    Exit Function
End Function
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top