GetOpenFilename is a method which is also an attribute of FSO, this method is used in VBA to find a certain file with a file name and select it, the important factor in this method is the path of the file name provided to open it, we can either pass the path of the file name in the function or we can ask the user to present a file path to select it.
Excel VBA Application.GetOpenFilename
There are situations where we need to access the specific file name, and this can be possible with VBA coding. In order to access to the file, we need to mention the folder path as well and file name along with its file extension. In order to get the file name, many coders will give VBA input box as the options 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 don’t always remember the file path, backslashes to separate one folder from another folder, file names, and extension of the files. This makes the input given by the user messier, and in the end, everything will be screwed 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.”
In this article, we will show you how to use VBA GetOpenFileName to get the file name without any 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 actually present a dialogue box in front of the user to select the file in the required folder. “GetOpenFileName” will copy the file location along with file name and file extension.
Syntax of GetOpenFilename in Excel VBA
Take a look at the syntax of the “GetOpenFilename” method.
4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
- 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.” No other files will be displayed. If you ignore all kinds of files will be displayed.
- Filter Index: With this, we restrict the user to select the file type. We can list specify the number of filters to be 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 the examples of VBA Application.GetOpenFilename.
Let’s write a code to get the file name and path address.
Step 1: Start the subroutine.
Sub GetFile_Example1() End Sub
Step 2: Declare a variable as String.
Sub GetFile_Example1() Dim FileName As String End Sub
Step 3: For this variable, we will assign the GetOpenFileName.
Sub GetFile_Example1() Dim FileName As String FileName = Application.GetOpenFilename() End Sub
As of now, I have ignored all the parameters.
Step 4: Now show the result of the variable in the message box.
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 dialogue box to select the file.
I will select any one file and click on ok.
As soon as I select the file, I got a message box in VBA like this. It shows the full folder path and selected excel file name along with file extension.
As we can see in the above image, we could see all kinds of files. Now I will add the first parameter i.e., File Filter as “Excel Files,*.xlsx.”
Sub GetFile_Example1() Dim FileName As String FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xlsx") MsgBox FileName End Sub
Now, if I run this code using the F5 key or manually, I 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 & extension of the file.
This has been a guide to VBA GetOpenFilename. Here we learn how to use VBA application.getopenfilename method to select the files from the folders along with examples. Below are some useful excel articles related to VBA –