If you are writing a VBA macro in Excel, you may have a need to allow the user to specify a file they want from the disk. Fortunately, you can access the standard Open dialog box from within VBA and use it to return just a file name. The following example subroutine shows how this is done:
Sub GetFName() Dim FName As Variant Dim Msg As String FName = Application.GetOpenFilename() If FName <> False Then Msg = "You chose " & FName MsgBox Msg Else 'Cancel was pressed End If End Sub
When you run this macro, you will see the standard Open dialog box used in Excel. The user can select a file, and when they click on Open, the file name (including the full path) is assigned to the variable FName. If the user clicks on the Cancel button, then FName is set equal to False. (Thus the test for that in the code.)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11635) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Getting a File Name.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...
Discover MoreYou can create macros that run whenever Excel detects a certain event happening within an entire workbook. This tip ...
Discover MoreWhen creating macros, it is often necessary to change from one type of data to another. Here's how you can change from a ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-01-04 11:06:56
J. Woolley
@Greg Van Hook
To browse for a folder name, try this:
Sub GetFolder()
Dim Folder As Variant
Dim Msg As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> False Then
Folder = .SelectedItems(1)
Msg = "You chose " & Folder
MsgBox Msg
Else
'Cancel was pressed
End If
End With
End Sub
You can find more information at Chip Pearson's legacy web site (which was recently restored): http://www.cpearson.com/excel/BrowseFolder.aspx
2020-01-03 10:37:50
Greg Van Hook
Is there an easy way to change this code to collect only the file path and not the file name? I have a routine that creates individual files from tabs in a workbook and I want to specify a specific file directory path to save the files to. The path changes each time the routine is run.
Thanks,
Greg
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments