WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Blog
  • Free Video Tutorials
  • Courses
  • All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA GetOpenFilename

VBA GetOpenFilename

By Jeevan A YJeevan A Y | Reviewed By Dheeraj VaidyaDheeraj Vaidya, CFA, FRM

By Jeevan A Y

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.

VBA GetOpenFilename

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.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

VBA GetOpenFilename syntax

  • 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.

You can download this VBA GetOpenFilename Excel Template here – VBA GetOpenFilename Excel Template

Let’s write a code to get the file name and path address.

Step 1: Start the subroutine.

Code:

Sub GetFile_Example1()

End Sub

VBA GetOpenFilename Example 1

Step 2: Declare a variable as String.

Code:

Sub GetFile_Example1()

   Dim FileName As String

End Sub

VBA GetOpenFilename Example 1-1

Step 3: For this variable, we will assign the GetOpenFileName.

Code:

Sub GetFile_Example1()

  Dim FileName As String

  FileName = Application.GetOpenFilename()

End Sub

VBA GetOpenFilename Example 1-2

As of now, I 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

Example 1-3

Now run the code through the excel shortcut key F5 or manually, it will show the below dialogue box to select the file.

Example 1-4

I will select any one file and click on ok.

VBA GetOpenFilename Example 1-5

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.”

Code:

Sub GetFile_Example1()

  Dim FileName As String

  FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xlsx")

  MsgBox FileName

End Sub

VBA GetOpenFilename Example 1-6

Now, if I run this code using the F5 key or manually, I will see only Excel files with the extension “xlsx.”

Example 1-7

Like this, we can use the “VBA Application.GetOpenFileName” method to get the folder path along with the File Name & extension of the file.

Recommended Articles

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 –

  • VBA FileCopy
  • VBA FileDialog
  • Operators in VBA
  • Call Subroutine in VBA
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>

Category iconExcel, VBA & Power BI,  Learn VBA

Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA GetOpenFilename Excel Template

Special Offer - VBA Training Course (6 courses, 35+ hours video) View More