Table Of Contents
Excel VBA Application.GetOpenFilename
There are situations where we need to access the specified file name, which can be possible with VBA coding. To access the file, we need to mention the folder path and file name along with its file extension. To get the file name, many coders will give the VBA input box as the option to enter the file path and file name. But this is not a good option to practice because when you present an input box in front of the user, they do not always remember the file path, backslashes to separate one folder from another folder, file names, and extension of the files. It makes the input the user gives messier. In the end, everything will screw up, even if there is a small space character mistake. The best way is to replace the input box with VBA's method called "GetOpenFileName."
This article will show you how to use VBA GetOpenFileName to get the file name without errors.
What Does GetOpenFilename do in Excel VBA?
VBA "GetOpenFileName" allows the user to select the file from the computer we are working on without opening the file.
With the help of the "GetOpenFileName" method, we can present a dialog box in front of the user to select the file in the required folder. "GetOpenFileName" will copy the file location along with the file name and file extension.
Syntax of GetOpenFilename in Excel VBA
Take a look at the syntax of the "GetOpenFilename" method.
- File Filter: In this argument, we can specify what kind of files to be displayed to select. For example, if you mention "Excel Files,*.xlsx," it will display only Excel Files saved with the excel extension "xlsx." It will display no other files. However, if you ignore it, it will display all kinds of files.
- Filter Index: With this, we restrict the user from selecting the file type. We can specify the number of filters visible under File Filter.
- Title: It shows the select file dialogue box title.
- Button Text: This is only for Macintosh.
- Multi-Select: TRUE if you want to select multiple files or else FALSE. The default value is FALSE.
Example of GetOpenFilename in Excel VBA
Below are examples of VBA Application.GetOpenFilename.
Let us write a code to get the file name and path address.
Step 1: Start the subroutine.
Code:
Sub GetFile_Example1() End Sub
Step 2: Declare a variable as String.
Code:
Sub GetFile_Example1() Dim FileName As String End Sub
Step 3: For this variable, we will assign the GetOpenFileName.
Code:
Sub GetFile_Example1() Dim FileName As String FileName = Application.GetOpenFilename() End Sub
As of now, we have ignored all the parameters.
Step 4: Now show the result of the variable in the message box.
Code:
Sub GetFile_Example1() Dim FileName As String FileName = Application.GetOpenFilename() MsgBox FileName End Sub
Now run the code through the excel shortcut key F5 or manually. It will show the below dialog box to select the file.
We will select any one file and click on "OK."
When we select the file, we get a message box in VBA like this. It shows the full folder path, selected Excel file name, and file extension.
As seen in the above image, we could see all kinds of files.
Now we will add the first parameter, i.e., File Filter, as "Excel Files,*.xlsx."
Code:
Sub GetFile_Example1() Dim FileName As String FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xlsx") MsgBox FileName End Sub
If we run this code using the F5 key or manually. We will see only Excel files with the extension "xlsx."
Like this, we can use the "VBA Application.GetOpenFileName" method to get the folder path along with the file name and extension.