Sometimes we may want to open an existing workbook using VBA.
You can set the opened workbook to an object, so that it is easy to
refer your workbook to do further tasks.Open a closed workbook is a very common task to perform using VBA.
To open a
closed workbook you can use the Workbooks.Open function which
takes the name, of the workbook along with the complete path to open, as an
argument.
Below VBA code example will give you the more clarity on this:
Workbooks.Open Filename:="File_Name" 'Preferred Syntax
OR
Workbooks.Open "File_Name"
Where "File_Name" will be the complete path of the file i.e.
"C:\Documents\Info.xlsx". So, to open this file the code would be as
follows:
Sub Open_Workbook()
Workbooks.Open Filename:="C:\Documents\Info.xlsx"
End Sub
Note: you can only open "Info.xlsx" without specifying the file's path if it's stored in your default file
location. To change the default file location, on the File tab, click Options,
Save.
You can also use the GetOpenFilename method of the Application
object to display the standard Open dialog box.
Dim MyFile As String
MyFile = Application.GetOpenFilename()
A dialog box will open as shown below, Select a file and click
Open.
Note: GetOpenFilename doesn't actually open the file.
No comments:
Post a Comment