Insert File Name

michaelp1029

Board Regular
Joined
Jul 15, 2008
Messages
63
Here's what I need to do. I want to click a button so that an "Open" dialog box appears. Then I need to select a file and have the folder path and file name appear in cell A60. But the thing is the file name and folder path I choose will be different each time I use it. Help?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:

Code:
Sub test()
Dim fn As String
fn = Application.GetOpenFilename
Range("A60") = fn
End Sub

Dom
 
Upvote 0
From the view menu, choose toolbars -> control toolbox.

Insert your command button and then right click and choose 'view code' from the menu

paste the following

Code:
Private Sub CommandButton1_Click()
    With Application.FileDialog(1)
        If .Show = -1 Then
            [A60] = .SelectedItems(1)
        End If
    End With
End Sub

type alt+F11 to close the vb window then deselect the 'design' button (top left) on the control toolbox.

Before you do the above, you can edit the properties of the button to give it a suitable name
 
Upvote 0
You could also try this in case they select cancel:

Code:
Sub fileNamePath()
    Application.FileDialog(msoFileDialogOpen).Show
    If Application.FileDialog(msoFileDialogOpen).SelectedItems.Count > 0 Then Range("A60").Value = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
End Sub
Or test Domski's approach for a false to mask the false in A60.

Hope that helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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