REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

VBA code to open Excel File will help you to open Excel Workbook using VBA. VBA open excel file Examples to show you use of Workbook.Open method in Excel VBA 2003, 2007, 2010, 2013.

vba open excel file

VBA Code to Open an Excel File using Workbooks.Open Method

We can open Excel Workbook using Workbooks.Open Method. Following are the VBA Examples and syntax of VBA Code to Open an Excel File.

VBA Code to Open an Excel File using Workbooks.Open Method: Syntax

Here is the syntax to Open an Excel File using VBA. Here we need to pass the Workbook path to open.


Workbooks.Open("C:temptest.xlsx")

VBA Code to Open an Excel File using Workbooks.Open Method: Examples

The following Excel VBA example will open the test.xlsx file in the C:temp folder. Here we are opening the workbook and setting to an object. This will help us to re use the open workbook in the program.

Sub sbVBA_To_Open_Workbook()
    Dim wb As Workbook
    Set wb = Workbooks.Open("C:temptest.xlsx")
End Sub

VBA Code to Open an Excel File Explained:
‘Starting procedure to write VBA code to open excel file
Sub sbVBA_To_Open_Workbook()

‘Declaring the wb variable as workbook
Dim wb As Workbook

‘Opening a workbook and setting to the wb object for further use
Set wb = Workbooks.Open(“C:temptest.xlsx”)

‘Ending the sub procedure
End Sub

VBAopen excel file: Why we are using an Object

This is is the best approach to opening and assigning workbook to an object. This will help us to re use the Opened workbook and deal with its worksheets, ranges and other objects. The following example will show you how to access the different examples of opened workbook by setting and assigning to an object.

The below VBA Code example will get the Name of the Opened Workbook
We are using the Workbook.Name property to get the workbook name of the opened workbook.

Sub sbVBA_To_Open_WorkbookName()
    Dim wb As Workbook
    
    Set wb = Workbooks.Open("C:temptest.xlsx")
    
    'This will return the workbook name
    MsgBox wb.Name
End sub

The below VBA Code will get the count of worksheets in the Opened Workbook
We are using the Worksheets.Count property of workbook to get the number of worksheets in the opened workbook.

Sub sbVBA_To_Open_Workbook_Worksheets_Count()
    Dim wb As Workbook
    
    Set wb = Workbooks.Open("C:temptest.xlsx")
    
    'This will return number of worksheets in the workbook
    MsgBox wb.Worksheets.Count
End sub

The below VBA Code example will get the first worksheet Name of the Opened Workbook
We are using the Worksheet.Name property of workbook to get the name of the of worksheets in the opened workbook.

Sub sbVBA_To_Open_Workbook_Worksheets_Count()
    Dim wb As Workbook
    
    Set wb = Workbooks.Open("C:temptest.xlsx")
    
    'This will return the first sheet name of the workbook
    MsgBox wb.Sheets(1).Name
End sub

The below VBA Code example will get the Range C2 value of the Worksheet “Main” of the Opened Workbook
We are using the Worksheet.Range object of workbook to get the Range value of the worksheets in the opened workbook.

Sub sbVBA_To_Open_Workbook_Worksheets_Count()
    Dim wb As Workbook
    
    Set wb = Workbooks.Open("C:temptest.xlsx")
    
    'This will return the Range C2 value of the worksheet "Main"
    MsgBox wb.Sheets("Main").Range("C2")
End sub
Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBATags: , Last Updated: June 17, 2022

One Comment

  1. Sunil March 14, 2016 at 12:04 PM - Reply

    Thanks sir

Leave A Comment