Home / VBA / How to Activate a Sheet using VBA
Let’s say you are working with multiple worksheets, and for you, it’s hard to navigate to a sheet using a tab. In this situation, you can use a VBA code to activate any worksheet.
And, to write a VBA code for this you need to use Worksheet.Activate Method. In this post, I’d like to share with you a simple way to write this code to activate or select a worksheet. Before you write this code, you need to understand this method.
In this method, you can specify the worksheet’s name or number which you want to activate. Let’s say you need to activate sheet1, then the code will be:
Worksheets("Sheet1").Activate
Or you can use sheet numbers as well.
Worksheets("1").Activate
So the final code will be:
Sub ActivateSheet1()
Worksheets("Sheet1").Activate
End Sub
Examples: Activate a Worksheet with VBA
In the real life, you can use this method in different ways. Here are some of them.
1. Activate a Worksheet on Opening
If you want to activate a specific worksheet every time when you open the workbook then you name that VBA code auto_open.
Sub auto_open()
Worksheets("Sheet1").Activate
End Su
2. Activate a Worksheet and Hide all other
Maybe you want to navigate to a worksheet and hide all the other worksheets from the workbook. You can do this by using the below code.
Sub HideWorksheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
Change the sheet name from the above code to use it further.
Must Read Next
- VBA to Create New Sheet
- How to Record a Macro in Excel
- VBA Option Explicit
VBA is one of the Advanced Excel Skills, and if you are getting started with VBA, make sure to check out there (What is VBA, and Useful Macro Examples and VBA Codes).
VBA Activate Worksheet in Excel
VBA Activate Worksheet method is used to makes the current sheet as active sheet. Here we are the examples using Activate method of worksheet object in VBA. It is very frequently used method while writing VBA macros. We also see the VBA ActiveSheet object with examples.
- Why we need to Activate a Worksheet?
- VBA Activate Worksheet – Syntax
- VBA Activate Worksheet Method: Example 1
- VBA Activate Worksheet Method: Example 2
- VBA ActiveSheet Object
- Set ActiveSheet in Excel VBA
- VBA Activate Worksheet Method- Best Approach
- VBA Activate Worksheet Method- Instructions
When we need to use Activate Worksheet method in VBA?
We use Activate worksheet method to activate current sheet as active sheet. When we are working with multiple sheets in a workbook, if we want to move or go to another sheet in the same workbook or another workbook we use activate worksheet method.
VBA Activate Worksheet Method- Syntax
Here is the example syntax to activate Worksheet using VBA. You can use either a Worksheet name or Worksheet number. Always best practice is to use sheet name.
Worksheets(“Your Worksheet Name”).Activate
‘Or
Worksheets(“Worksheet Number”).Activate
Where Activate is the method of Workbook object is used to makes current sheet as active sheet.
VBA Activate Worksheet – with Name: Example 1
Please see the below VBA codes to activate Worksheet. In this example we are activating a Worksheet named “Project1”.
Sub Activate_Sheet() Worksheets("Project1").Activate 'Or Sheets("Project1").Activate End Sub
VBA Activate Worksheet Method– with Number: Example 2
Please see the below VBA code or macro procedure to activate Worksheet. In this example we are activating first Worksheet in the active workbook.
Sub Activate_Sheet_BasedOnIndex() Worksheets(1).Activate 'Or Sheets(1).Activate End Sub
VBA ActiveSheet Object
We can refer the currently activated worksheet using Excel VBA ActiveSheet object. ActiveSheet VBA object is very usefull while automating tasks and working on currently active sheet in the active workbook window. If you ignore the ActiveSheet object while refering any other object like range or chart, VBA will treat the ActiveSheet as the current sheet by default. For example: Following are the two macro statements both will refer the active sheet.
ActiveSheet.Range("A1")="Some Value" Range("A1")="Some Value"
both the above statements print some value at Range A1 of Activesheet.
Set ActiveSheet in Excel VBA
It is very convinient refer the Active Sheet in Excel VBA by setting into a Variable. We can assign and set ActiveSheet to an object and refer at any place of the procedure. Here is the syntax to Set ActiveSheet in VBA.
Sub sbSetAcitveSheetVBA() Dim ws As Worksheet Set ws = ActiveSheet ws.Range("A1") = "Some Value" End Sub
This code will print the some value at Range A1 of Activesheet.
VBA Activate Worksheet Method- Best Approach
Note: Always better to use the Worksheet name, instead of Worksheet number. The best is to assign the Worksheet to an object and then do whatever task you want to do with that particular Worksheet object.
When working with multiple Worksheets, you should refer the Worksheet with exact Worksheet name to correctly update your data into target Worksheet. Create Worksheet object and refer the Worksheet with the object whenever you require.
Let us see another example to understand the accessing the Worksheets using objects. You do not need to activate Worksheet to deal with any Worksheet.
Sub sb_Activate_Workbook_Object() 'Declare the objects here Dim wsMain As Worksheet, ws_A As Worksheet 'Set the worksheet to Object Set wsMain = ThisWorkbook Set ws_A = Worksheets("Test") 'Now deal with your worksheets ws_A.Range("A1") = wsMain.Sheet1.Range("A1") End Sub
VBA Activate Worksheet Method- Instructions
Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:
- Open an Excel Worksheet
- Press Alt+F11 :This will Open the VBA Editor. Otherwise, you can open it from the Developer Tab
- Insert a Module from Insert Menu
- Copy the above code for activating worksheet and Paste in the code window(VBA Editor)
- Save the file as macro enabled Worksheet
- Press ‘F5’ to run it or Keep Pressing ‘F8’ to debug the code line by line and check how the sheet is activating.
Related Resource
You have seen how to activate worksheets using Excel VBA and referring the AciveSheet. Following are the example macros to Activate Excel Workbook and Range.
- VBA to Activate Range
- VBA to Activate Workbook
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
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management 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
-
-
- In this topic:
-
- When we need to use Activate Worksheet method in VBA?
- VBA Activate Worksheet Method- Syntax
- VBA Activate Worksheet – with Name: Example 1
- VBA Activate Worksheet Method– with Number: Example 2
- VBA ActiveSheet Object
- Set ActiveSheet in Excel VBA
- VBA Activate Worksheet Method- Best Approach
- VBA Activate Worksheet Method- Instructions
VBA Reference
Effortlessly
Manage Your Projects
120+ Project Management Templates
Seamlessly manage your projects with our powerful & multi-purpose templates for project management.
120+ PM Templates Includes:
One Comment
-
jojo
February 14, 2019 at 7:15 PM — ReplyHello, in your last example you declare wsMain as WorkSheet and go on to set it as a Workbook and treat it as a Workbook, it this possible to do?
Thanks for your very helpful work.
Effectively Manage Your
Projects and Resources
ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.
We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.
Project Management
Excel VBA
Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.
Page load link
Go to Top
Return to VBA Code Examples
In this Article
- ActiveSheet
- Activate Worksheet (Setting the ActiveSheet)
- ActiveSheet Name
- Selected Sheets vs ActiveSheet
- Select Worksheet
- Select Worksheet by Tab Name
- Select Worksheet by Index Number
- Select Worksheet With VBA Code Name
- Select Current Worksheet
- More Activate / Select Sheet Examples
- Set ActiveSheet to Variable
- Change ActiveSheet Name
- With ActiveSheet
- Loop Through Selected Sheets
- GoTo Next Sheet
- VBA Coding Made Easy
This article will discuss the ActiveSheet object in VBA. It will also discuss how to activate, select, and go to Worksheets (& much more). Read our full VBA Worksheets Guide for more information about working with worksheets in VBA.
ActiveSheet
In VBA, ActiveSheet refers to the currently active Worksheet. Only one Sheet may be active at a time.
Activate Worksheet (Setting the ActiveSheet)
To set the ActiveSheet use Worksheet.Activate:
Worksheets("Input").Activate
The Activate Sheet command will actually “go to” the sheet, changing the visible Sheet.
The above example uses the Sheet (Tab) name. Instead you can use the VBA code name for the worksheet:
Sheet1.Activate
ActiveSheet Name
To get the ActiveSheet Name:
msgbox ActiveSheet.name
Selected Sheets vs ActiveSheet
At any point in time, only one Sheet can be the ActiveSheet. However, multiple Worksheets can be selected at once.
When multiple Worksheets are selected only the “top-most” Worksheet is considered active (the ActiveSheet).
Select Worksheet
If you would like to select a worksheet instead of activating it. Use .Select instead.
Select Worksheet by Tab Name
This selects a Worksheet based on it’s Sheet Tab Name
Sheets("Input").Select
Select Worksheet by Index Number
This selects a Worksheet based on it’s position relative to other tabs
Worksheets(1).Select
Select Worksheet With VBA Code Name
Sheet1.Select
Selecting worksheets by code name can prevent errors caused by worksheet name changes.
Select Current Worksheet
To select the current Worksheet, use the ActiveSheet object:
ActiveSheet.Select
More Activate / Select Sheet Examples
VBA Programming | Code Generator does work for you!
Set ActiveSheet to Variable
This will assign the ActiveSheet to a Worksheet Object Variable.
Dim ws As Worksheet
Set ws = ActiveSheet
Change ActiveSheet Name
This will change the ActiveSheet Name.
ActiveSheet.Name = "NewName"
With ActiveSheet
Using the With Statement allows you to streamline your code when working with objects (such as Sheets or ActiveSheet).
With ActiveSheet
.Name = "StartFresh"
.Cells.Clear
.Range("A1").Value = .Name
End With
Notice how you don’t need to repeat “ActiveSheet” before each line of code. This can be a huge time saver when working with a long list of commands.
Loop Through Selected Sheets
The following macro will Loop through all selected sheets, displaying their names.
Sub GetSelectedSheetsName()
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
MsgBox ws.Name
Next ws
End Sub
GoTo Next Sheet
This code will go to the next Sheet. If the ActiveSheet is the last Sheet, then it will go to the first Sheet in the Workbook.
If ActiveSheet.Index = Worksheets.Count Then
Worksheets(1).Activate
Else
ActiveSheet.Next.Activate
End If
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
<<Return to VBA Examples
While working in VBA we sometimes refer to another sheet or use another sheet’s properties, suppose we are in sheet 1 working but we want a value from cell A2 in sheet 2, if we refer to sheet 2’s value without activating the sheet first then we will not be able to access the value so to activate a sheet in VBA we use worksheet property as Worksheets(“Sheet2”).Activate.
In Excel, we always work with worksheets. Worksheets have their name to identify better. In regular spreadsheet workings, we directly navigate through shortcut keysAn Excel shortcut is a technique of performing a manual task in a quicker way.read more, or select the sheet by clicking on them. However, in VBA, it is not that easy. First, we need to specify the sheet name we are referring to. Then, we can use the “Select” method to select the sheet.
Table of contents
- Excel VBA Activate Sheet
- What is VBA Activate Method?
- Example #1 – Activate Sheet by its Index Number
- Example #2 – Activate Sheet by its Name
- Example #3 – Activate Sheet from Another Workbook
- Activate Sheet vs. Select Sheet Method
- #1 – Activate Method
- #2 – Select Method
- Recommended Articles
- What is VBA Activate Method?
What is VBA Activate Method?
As the name says, it activates the specified worksheet. To activate the sheet, we need to mention the exact worksheet name using the worksheets object. For example, if you want to activate a ” Sales sheet,” you can use the code below.
Worksheets(“Sales”).Activate
Syntax
So, the syntax of the Activate method is as follows:
Worksheet (“Name of the Sheet”).Activate
Here, a worksheet is the object, and activates are the method.
You can download this VBA Activate Sheet Excel Template here – VBA Activate Sheet Excel Template
Example #1 – Activate Sheet by its Index Number
In Excel, we work with multiple sets of worksheets. Often, we need to move from one sheet to another to get the job done. In VBA, we can use the Activate method to activate the particular Excel sheet.
For example, we have created three sheets: “Sales 2015”, “Sales 2016”, and “Sales 2017.”
We can activate the sheets in two ways. One is by using sheet index numbers, and another is by using the sheet name.
If we want to select the second sheet, we will use the worksheet object and mention the sheet index number as 2.
Code:
Sub Activate_Example1() Worksheets(2).Activate End Sub
When you run the code using the F5 key or manually, this will activate the second sheet, “Sales 2016”.
If we want to activate the third sheet, we will use 3 as the sheet index number.
Code:
Sub Activate_Example1() Worksheets(3).Activate End Sub
It will activate the third sheet, “Sales 2017.”
Now, we will interchange the second and third sheets.
Technically, “Sales 2017” is our third sheet, and “Sales 2016 is our second sheet. So, now we will use the sheet index number as 3 and see what happens.
Code:
Sub Activate_Example1() Worksheets(3).Activate End Sub
In our view, it has to select the “Sales 2017” sheet, but it will select the “Sales 2016” sheet because in the order, “Sales 2016” is the third sheet.
So, activating the sheet by its name is always a safe option.
Example #2 – Activate Sheet by its Name
Now, we will see how to activate sheets by their name. In the place of a sheet index number, we need to mention the sheet name in double quotes.
Code:
Sub Activate_Example2() Worksheets("Sales 2016").Activate End Sub
When we run the code manually or using shortcut key F5, this would activate the sheet “Sales 2016” irrespective of the position in the workbook.
Not only the worksheets object, but we can also use the “Sheets” object to activate the sheet.
Below is the code.
Code:
Sub Activate_Example2() Sheets("Sales 2016").Activate End Sub
Worksheets can access only worksheets object and cannot access “Chart” sheets. However, if we use the Sheets object, we can access all the sheets in the workbook.
Example #3 – Activate Sheet from Another Workbook
Like how we need to mention the sheet name to activate the particular sheet, activating the sheet from another workbook also requires the “Workbook” name.
Code:
Sub Activate_Example3() Workbooks("Sales File.xlsx").Sheets("Sales 2016").Activate End Sub
It will activate the “Sales 2016” sheet from the workbook “Sales File.xlsx.”
Activate Sheet vs. Select Sheet Method
We can use methods to perform the same action, i.e., Activate and Select. However, there is a slight difference between these two methods.
#1 – Activate Method
By using the Activate method, we can only activate the specified worksheet.
For example, look at the below code.
Code:
Sub Activate_Example() Worksheets("Sales 2016").Activate End Sub
As we know, this code will select the worksheet “Sales 2016”.
#2 – Select Method
By using the Select method, we can perform other tasks as well.
Now, look at the below code.
Code:
This code not only activates the sheet “Sales 2016” but also selects the range of cells from A1 to A10.
Recommended Articles
This article has been a guide to VBA Activate Sheet. Here, we learn how to use the activate method in VBA to activate a particular Excel sheet, along with practical examples and downloadable templates. Below you can find some useful Excel VBA articles: –
- VBA TimeValue
- VBA DatePart Examples
- Option Explicit in VBA
- VBA Remove Duplicates
- VBA StatusBar
In this Excel VBA Tutorial, you learn how to activate workbooks with macros.
This Excel VBA Activate Workbook Tutorial is currently under development. Subscribe to the Power Spreadsheets Newsletter to get future updates to this Excel VBA Tutorial.
Use the following Table of Contents to navigate to the Section you’re interested in.
VBA Code to Activate This Workbook
To activate this workbook, use the following statement in the applicable procedure.
Process to Activate This Workbook with VBA
- Refer to this workbook (the workbook where the macro is stored).
- Activate the workbook.
Main VBA Constructs Used to Activate This Workbook
(1) Application.ThisWorkbook property.
Returns a Workbook object representing the workbook where the macro is stored (the workbook where the macro is running).
(2) Workbook object.
Represents an Excel workbook.
(3) Workbook.Activate method.
Activates the (first) window associated with a workbook.
Macro Example to Activate This Workbook
The macro below does the following:
- Activate this workbook (the workbook where the macro is stored).
- Maximize the active window.
Sub ActivateThisWorkbook() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'This procedure: '(1) Activates this workbook (the workbook where the macro is stored) '(2) Maximizes the active window 'Activate this workbook ThisWorkbook.Activate 'Maximize the active window ActiveWindow.WindowState = xlMaximized End Sub
Effects of Executing Macro Example to Activate This Workbook
The image below illustrates the effects of using the macro example. In this example:
- 2 workbooks (“Excel VBA Activate Workbook” and “Book1”) are open.
- The “Book1” workbook is the active workbook.
- The macro example is stored in the “Excel VBA Activate Workbook” workbook.
When the macro is executed, Excel:
- Activates this workbook (“Excel VBA Activate Workbook”; the workbook where the macro example is stored); and
- Maximizes the active window.
Excel VBA Activate Workbook by Filename
VBA Code to Activate Workbook by Filename
To activate a workbook by filename, use the following structure/template in the applicable statement.
Workbooks("Filename").Activate
Process to Activate Workbook by Filename with VBA
- Refer to the workbook to activate by filename.
- Activate the applicable workbook.
Main VBA Constructs Used to Activate Workbook by Filename
(1) Application.Workbooks property.
Returns a Workbooks collection representing all open workbooks.
(2) Workbooks object.
Represents all open workbooks.
(3) Workbooks.Item property.
Returns a specific Workbook object from the applicable Workbooks collection.
(4) Workbook object.
Represents an Excel workbook.
(5) Workbook.Activate method.
Activates the (first) window associated with a workbook.
Cues to Activate Workbook by Filename with VBA
- Consider whether you must wrap the workbook filename in double quotes and parentheses ((“Filename”)).
- As a general rule: Include the complete filename, including file extension (for ex., “Filename.xlsm”), of a previously saved workbook.
Macro Example to Activate Workbook by Filename
The macro below does the following:
- Activate the workbook with filename “Excel VBA Activate Workbook.xlsm”.
- Maximize the active window.
Sub ActivateWorkbookByFilename() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'This procedure: '(1) Activates the workbook with filename "Excel VBA Activate Workbook.xlsm" '(2) Maximizes the active window 'Activate the workbook with filename "Excel VBA Activate Workbook.xlsm" Workbooks("Excel VBA Activate Workbook.xlsm").Activate 'Maximize the active window ActiveWindow.WindowState = xlMaximized End Sub
Effects of Executing Macro Example to Activate Workbook by Filename
The image below illustrates the effects of using the macro example. In this example:
- 2 workbooks (“Excel VBA Activate Workbook.xlsm” and “Book1”) are open.
- The “Book1” workbook is the active workbook.
When the macro is executed, Excel:
- Activates the workbook with filename “Excel VBA Activate Workbook.xlsm”; and
- Maximizes the active window.
Excel VBA Activate Workbook with Partial Filename (with Left, Mid, or Right Functions)
VBA Code to Activate Workbook with Partial Filename (with Left, Mid, or Right Functions)
To activate a workbook with partial filename (with the Left, Mid, or Right functions), use the following structure/template in the applicable procedure.
VariableDeclarationStatement iWorkbook As Workbook For Each iWorkbook In Application.Workbooks If VbaBuiltInTextFunction(TextFunctionArgumentsIncludingWorkbookName) = "PartialWorkbookFilename" Then iWorkbook.Activate End If Next iWorkbook
Process to Activate Workbook with Partial Filename (with Left, Mid, or Right Functions)
- Declare iteration object variable (usually of the Workbook object data type).
- Loop through all open workbooks.
- Test whether the applicable part (for example, the beginning or end) of the name of the workbook (the loop is currently iterating through) matches the (known) partial filename (of the workbook to activate).
- If the condition is met (the applicable part of the name of the applicable workbook matches the partial filename of the workbook to activate), activate the workbook (the loop is currently iterating through).
Main VBA Constructs Used to Activate Workbook with Partial Filename (with Left, Mid, or Right Functions)
(1) Variable declaration statement.
One of the following 4 statements:
- Dim: Declares variables and allocates storage space.
- Private:
- Used at the module level.
- Declares module-level variables and allocates storage space.
- Public:
- Used at the module level.
- Declares global variables and allocates storage space.
- Static: Declares static variables and allocates storage space.
(2) Workbook object.
Represents an Excel workbook.
(3) For Each… Next statement.
Repeats a set of statements for each element in an array or collection.
(4) Application.Workbooks property.
Returns a Workbooks collection representing all open workbooks.
(5) Workbooks object.
Represents all open workbooks.
(6) Object variable.
A named storage location containing data (a reference to an object) that can be modified during procedure execution.
(7) If… Then… Else statement.
Conditionally executes a set of statements, depending on the value returned by a logical expression.
(8) VBA built-in text function.
One (or more) of the following VBA built-in text functions:
- Left: Returns a string containing a specific number of characters from the start (left side) of a string.
- Mid: Returns a string containing a specific number of characters from the middle (starting at a specific position) of a string.
- Right: Returns a string containing a specific number of characters from the end (right side) of a string.
- InStr: Returns the position of the first occurrence of one string inside another string.
(9) String.
A sequence of contiguous characters.
(10) Workbook.Activate method.
Activates the (first) window associated with a workbook.
Cues to Activate Workbook with Partial Filename (with Left, Mid, or Right Functions) with VBA
- Consider explicitly declaring:
- The iteration object variable.
- The data type (usually Workbook object) of the iteration object variable.
- As a general rule, declare the iteration object variable:
- Using the Dim statement; and
- As of the Workbook object data type.
- If the scope of the iteration object variable is module-level or global, follow the applicable rules for (module-level or global) variable declaration.
- Depending on the case you deal with, you may need to work with different versions of the If… Then… Else statement. Consider the following 4 basic versions of the If… Then… Else statement:
- If… Then. This is the version I use in the structure/template above.
- If… Then… Else.
- If… Then… ElseIf.
- If… Then… ElseIf… Else.
- The appropriate VBA built-in text function you work with depends on the case you deal with (for example, the structure and position of the known partial filename).
- As a general rule:
- The (main) string you use as one of the arguments of the VBA built-in text function (the string from which you extract characters) is the name of the applicable workbook (the For Each… Next statement is currently iterating through).
- Work with the Workbook.Name property to obtain the name of the applicable workbook. The Workbook.Name property returns a workbook’s name.
- When specifying the (known) partial filename, consider whether you must wrap this partial filename in double quotes (“PartialWorkbookFilename”).
Macro Example to Activate Workbook with Partial Filename (with Left, Mid, or Right Functions)
The macro below does the following:
- Declare an object variable (iWorkbook) of the Workbook object data type.
- Loop through all open workbooks.
- Test whether the first/leftmost 9 letters of the name of the workbook (the loop is currently iterating through) spell “Excel VBA”.
- If the condition is met (the first/leftmost 9 letters of the name of the applicable workbook spell “Excel VBA”), activate the workbook (the loop is currently iterating through).
Sub ActivateWorkbookPartialFilenameLeftMidRight() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'Declare iteration object variable Dim iWorkbook As Workbook 'Loop through all open workbooks For Each iWorkbook In Application.Workbooks 'Do the following: '(1) Test whether the first/leftmost 9 letters of the name of the workbook (the loop is currently iterating through) spell "Excel VBA" '(2) If the condition is met, activate the workbook (the loop is currently iterating through) If Left(iWorkbook.Name, 9) = "Excel VBA" Then iWorkbook.Activate Next iWorkbook End Sub
Effects of Executing Macro Example to Activate Workbook with Partial Filename (with Left, Mid, or Right Functions)
The image below illustrates the effects of using the macro example. In this example:
- 10 workbooks (“Excel VBA Activate Workbook.xlsm”, and “Book1” through “Book9”) are open.
- The “Book1” workbook is the active workbook.
When the macro is executed, Excel activates the workbook whose filename (Excel VBA Activate Workbook.xlsm) contains the (known) partial filename (Excel VBA).
Excel VBA Activate Workbook with Partial Filename (with InStr Function)
VBA Code to Activate Workbook with Partial Filename (with InStr Function)
To activate a workbook with partial filename (with the InStr function), use the following structure/template in the applicable procedure.
VariableDeclarationStatement iWorkbook As Workbook For Each iWorkbook In Application.Workbooks If InStr(1, iWorkbook.Name, "PartialWorkbookFilename", StringCompareMethodConstant) > 0 Then iWorkbook.Activate End If Next iWorkbook
Process to Activate Workbook with Partial Filename (with InStr Function)
- Declare iteration object variable (usually of the Workbook object data type).
- Loop through all open workbooks.
- Test whether the name of the workbook (the loop is currently iterating through) contains the (known) partial filename (of the workbook to activate).
- If the condition is met (the name of the applicable workbook contains the partial filename of the workbook to activate), activate the workbook (the loop is currently iterating through).
Main VBA Constructs Used to Activate Workbook with Partial Filename (with InStr Function)
(1) Variable declaration statement.
One of the following 4 statements:
- Dim: Declares variables and allocates storage space.
- Private:
- Used at the module level.
- Declares module-level variables and allocates storage space.
- Public:
- Used at the module level.
- Declares global variables and allocates storage space.
- Static: Declares static variables and allocates storage space.
(2) Workbook object.
Represents an Excel workbook.
(3) For Each… Next statement.
Repeats a set of statements for each element in an array or collection.
(4) Application.Workbooks property.
Returns a Workbooks collection representing all open workbooks.
(5) Workbooks object.
Represents all open workbooks.
(6) Object variable.
A named storage location containing data (a reference to an object) that can be modified during procedure execution.
(7) If… Then… Else statement.
Conditionally executes a set of statements, depending on the value returned by a logical expression.
(8) InStr function.
Returns the position of the first occurrence of one string inside another string.
Accepts the following 4 arguments:
- start:
- Optional argument.
- The starting position for the search.
- If omitted, the search begins at the first character.
- string1:
- Required argument.
- The string you search in.
- string2:
- Required argument.
- The string you search for.
- compare:
- Optional argument.
- The string comparison method.
- If omitted, the module’s Option Compare setting applies.
(9) Workbook.Name property.
Returns a workbook’s name.
(10) String.
A sequence of contiguous characters.
(11) Greater than operator (>).
Compares 2 expressions and returns True, False, or Null as follows:
- True: If Expression1 > Expression2.
- False: If Expression1 <= Expression2.
- Null: If (either) Expression1 or Expression2 are Null.
(12) Workbook.Activate method.
Activates the (first) window associated with a workbook.
Cues to Activate Workbook with Partial Filename (with InStr Function) with VBA
- Consider explicitly declaring:
- The iteration object variable.
- The data type (usually Workbook object) of the iteration object variable.
- As a general rule, declare the iteration object variable:
- Using the Dim statement; and
- As of the Workbook object data type.
- If the scope of the iteration object variable is module-level or global, follow the applicable rules for (module-level or global) variable declaration.
- Depending on the case you deal with, you may need to work with different versions of the If… Then… Else statement. Consider the following 4 basic versions of the If… Then… Else statement:
- If… Then. This is the version I use in the structure/template above.
- If… Then… Else.
- If… Then… ElseIf.
- If… Then… ElseIf… Else.
- As a general rule, set the arguments of the InStr function as follows:
- start: 1.
- string1: The name of the workbook the loop is currently iterating through, as returned by the Workbook.Name property.
- string2: The (known) partial filename.
- compare: vbBinaryCompare. vbBinaryCompare (usually) results in a case sensitive comparison.
- When specifying the (known) partial filename (as string2 argument of the InStr function), consider whether you must wrap this partial filename in double quotes (“PartialWorkbookFilename”).
Macro Example to Activate Workbook with Partial Filename (with InStr Function)
The macro below does the following:
- Declare an object variable (iWorkbook) of the Workbook object data type.
- Loop through all open workbooks.
- Test whether the name of the workbook (the loop is currently iterating through) contains “Excel VBA”.
- If the condition is met (the name of the applicable workbook contains “Excel VBA”), activate the workbook (the loop is currently iterating through).
Sub ActivateWorkbookPartialFilenameInStr() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'Declare iteration object variable Dim iWorkbook As Workbook 'Loop through all open workbooks For Each iWorkbook In Application.Workbooks 'Do the following: '(1) Test whether the name of the workbook (the loop is currently iterating through) contains "Excel VBA" '(2) If the condition is met, activate the workbook (the loop is currently iterating through) If InStr(1, iWorkbook.Name, "Excel VBA", vbBinaryCompare) > 0 Then iWorkbook.Activate Next iWorkbook End Sub
Effects of Executing Macro Example to Activate Workbook with Partial Filename (with InStr Function)
The image below illustrates the effects of using the macro example. In this example:
- 10 workbooks (“Excel VBA Activate Workbook.xlsm”, and “Book1” through “Book9”) are open.
- The “Book1” workbook is the active workbook.
When the macro is executed, Excel activates the workbook whose filename (Excel VBA Activate Workbook.xlsm) contains the (known) partial filename (Excel VBA).
Excel VBA Activate Workbook Using Wildcard
VBA Code to Activate Workbook Using Wildcard
To activate a workbook using a wildcard, use the following structure/template in the applicable procedure.
VariableDeclarationStatement Dim iWorkbook As Workbook For Each iWorkbook In Application.Workbooks If iWorkbook.Name Like "WorkbookNameUsingWildcard" Then iWorkbook.Activate End If Next iWorkbook
Process to Activate Workbook Using Wildcard
- Declare iteration object variable (usually of the Workbook object data type).
- Loop through all open workbooks.
- Test whether the name of the workbook (the loop is currently iterating through) is like the (known) filename (of the workbook to activate). Use wildcards (as necessary) to specify the filename of the workbook to activate.
- If the condition is met (the name of the applicable workbook is like the filename -including any wildcards- of the workbook to activate), activate the workbook (the loop is currently iterating through).
Main VBA Constructs Used to Activate Workbook Using Wildcard
(1) Variable declaration statement.
One of the following 4 statements:
- Dim: Declares variables and allocates storage space.
- Private:
- Used at the module level.
- Declares module-level variables and allocates storage space.
- Public:
- Used at the module level.
- Declares global variables and allocates storage space.
- Static: Declares static variables and allocates storage space.
(2) Workbook object.
Represents an Excel workbook.
(3) For Each… Next statement.
Repeats a set of statements for each element in an array or collection.
(4) Application.Workbooks property.
Returns a Workbooks collection representing all open workbooks.
(5) Workbooks object.
Represents all open workbooks.
(6) Object variable.
A named storage location containing data (a reference to an object) that can be modified during procedure execution.
(7) If… Then… Else statement.
Conditionally executes a set of statements, depending on the value returned by a logical expression.
(8) Workbook.Name property.
Returns a workbook’s name.
(9) Like operator.
Compares a string against a pattern and returns True, False, or Null as follows:
- True: If the string matches the pattern.
- False: If the string doesn’t match the pattern.
- Null: If (either) the string or the pattern are Null.
You can combine the following elements when specifying the pattern:
- Wildcard characters.
- Character lists.
- Character ranges.
(10) String.
A sequence of contiguous characters.
(11) Workbook.Activate method.
Activates the (first) window associated with a workbook.
Cues to Activate Workbook Using Wildcard with VBA
- Consider explicitly declaring:
- The iteration object variable.
- The data type (usually Workbook object) of the iteration object variable.
- As a general rule, declare the iteration object variable:
- Using the Dim statement; and
- As of the Workbook object data type.
- If the scope of the iteration object variable is module-level or global, follow the applicable rules for (module-level or global) variable declaration.
- Depending on the case you deal with, you may need to work with different versions of the If… Then… Else statement. Consider the following 4 basic versions of the If… Then… Else statement:
- If… Then. This is the version I use in the structure/template above.
- If… Then… Else.
- If… Then… ElseIf.
- If… Then… ElseIf… Else.
- Use the Like operator to compare the string returned by the Workbook.Name property (the name of the workbook the loop is currently iterating through) against a string with the filename (including any wildcards) of the workbook to activate.
- Use the following wildcards when specifying the pattern the Like operator works with:
- ?: Represents any single character.
- *: Represents any sequence of 0 or more characters.
- When specifying the pattern the Like operator works with (the filename -including any wildcards- of the workbook to activate), consider whether you must wrap this partial filename in double quotes (“WorkbookNameUsingWildcard”).
Macro Example to Activate Workbook Using Wildcard
The macro below does the following:
- Declare an object variable (iWorkbook) of the Workbook object data type.
- Loop through all open workbooks.
- Test whether the name of the workbook (the loop is currently iterating through) has the following structure:
- Starts with “Excel VBA”;
- Followed by any sequence of characters; and
- Ends with the “.xlsm” file extension.
- If the condition is met (the name of the applicable workbook is like the specified filename, including the * wildcard), activate the workbook (the loop is currently iterating through).
Sub ActivateWorkbookUsingWildcard() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'Declare iteration object variable Dim iWorkbook As Workbook 'Loop through all open workbooks For Each iWorkbook In Application.Workbooks 'Do the following: '(1) Test whether the name of the workbook (the loop is currently iterating through) has the following structure: '(1) Starts with "Excel VBA" '(2) Followed by any sequence of characters '(3) Ends with the ".xlsm" file extension '(2) If the condition is met, activate the workbook (the loop is currently iterating through) If iWorkbook.Name Like "Excel VBA*.xlsm" Then iWorkbook.Activate Next iWorkbook End Sub
Effects of Executing Macro Example to Activate Workbook Using Wildcard
The image below illustrates the effects of using the macro example. In this example:
- 10 workbooks (“Excel VBA Activate Workbook.xlsm”, and “Book1” through “Book9”) are open.
- The “Book1” workbook is the active workbook.
When the macro is executed, Excel activates the workbook whose filename (Excel VBA Activate Workbook.xlsm) has the following structure:
- Starts with “Excel VBA”;
- Followed by any sequence of characters; and
- Ends with the “.xlsm” file extension.
Excel VBA Activate Workbook and Worksheet
VBA Code to Activate Workbook and Worksheet
To activate a workbook and a worksheet, use the following structure/template in the applicable statement.
WorkbookObjectReference.WorksheetObjectReference.Activate
Process to Activate Workbook and Worksheet with VBA
- Refer to the workbook and worksheet to activate.
- Activate the applicable worksheet.
Main VBA Constructs Used to Activate Workbook and Worksheet
(1) Workbook object.
Represents an Excel workbook.
(2) Workbook.Worksheets property.
Returns a Sheets collection representing all worksheets in the applicable workbook.
(3) Worksheets object.
Represents all worksheets in the applicable workbook.
(4) Worksheets.Item property.
Returns a specific Worksheet object from the applicable Worksheets collection.
(5) Worksheet object.
Represents a worksheet.
(6) Worksheet.Activate method.
Activates the applicable worksheet.
Cues to Activate Workbook and Worksheet with VBA
- Work with (among others) one of the following VBA constructs (or groups of constructs) to return a Workbook object:
- Application.Workbooks property, Workbooks object, and Workbooks.Item property.
- Application.ThisWorkbook property.
- As a general rule, identify the applicable Worksheet object (inside the Worksheets collection) using one of the following:
- An index number. The index number:
- Represents the position of the Worksheet object in the Worksheets collection.
- Is:
- Wrapped in parentheses ((IndexNumber)).
- Included after the reference to the Worksheets collection (Worksheets(IndexNumber)).
- The Worksheet object’s name (Worksheet.Name property). The name is:
- Wrapped in:
- Double quotes (“WorksheetName”); and
- Parentheses ((“WorksheetName”)).
- Included after the reference to the Worksheets collection (Worksheets(“WorksheetName”)).
- Wrapped in:
- An index number. The index number:
- You can (also) use a Worksheet object’s CodeName property (Worksheet.CodeName property) to refer to the applicable worksheet.
Macro Example to Activate Workbook and Worksheet
The macro below does the following:
- Activate:
- The “Excel VBA Activate Workbook.xlsm” workbook; and
- The “Activate Workbook and Worksheet” worksheet.
- Maximize the active window.
Sub ActivateWorkbookAndWorksheet() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'This procedure: '(1) Activates the "Activate Workbook and Worksheet" worksheet in the "Excel VBA Activate Workbook.xlsm" workbook '(2) Maximizes the active window 'Activate the "Activate Workbook and Worksheet" worksheet in the "Excel VBA Activate Workbook.xlsm" workbook Workbooks("Excel VBA Activate Workbook.xlsm").Worksheets("Activate Workbook and Worksheet").Activate 'Maximize the active window ActiveWindow.WindowState = xlMaximized End Sub
Effects of Executing Macro Example to Activate Workbook and Worksheet
The image below illustrates the effects of using the macro example. In this example:
- 2 workbooks (“Excel VBA Activate Workbook.xlsm” and “Book1”) are open.
- The “Book1” workbook is the active workbook.
- The “Sheet1” worksheet is the active worksheet in both (“Excel VBA Activate Workbook.xlsm” and “Book1”) workbooks.
When the macro is executed, Excel:
- Activates:
- The “Excel VBA Activate Workbook.xlsm” workbook; and
- The “Activate Workbook and Worksheet” worksheet.
- Maximizes the active window.
Excel VBA Activate Workbook and Chart Sheet
VBA Code to Activate Workbook and Chart Sheet
To activate a workbook and a chart sheet, use the following structure/template in the applicable statement.
WorkbookObjectReference.ChartObjectReference.Activate
Process to Activate Workbook and Chart Sheet with VBA
- Refer to the workbook and chart sheet to activate.
- Activate the applicable chart sheet.
Main VBA Constructs Used to Activate Workbook and Chart Sheet
(1) Workbook object.
Represents an Excel workbook.
(2) Workbook.Charts property.
Returns a Sheets collection representing all chart sheets in the applicable workbook.
(3) Charts object.
Represents all chart sheets in the applicable workbook.
(4) Charts.Item property.
Returns a specific Chart object from the applicable Charts collection.
(5) Chart object.
Represents a chart in a workbook. The chart can be either of the following:
- A chart sheet.
- An embedded chart (not the subject of this Section).
(6) Chart.Activate method.
Activates the applicable chart.
Cues to Activate Workbook and Chart Sheet with VBA
- Work with (among others) one of the following VBA constructs (or groups of constructs) to return a Workbook object:
- Application.Workbooks property, Workbooks object, and Workbooks.Item property.
- Application.ThisWorkbook property.
- As a general rule, identify the applicable Chart object (inside the Charts collection) using one of the following:
- An index number. The index number:
- Represents the position of the Chart object in the Charts collection.
- Is:
- Wrapped in parentheses ((IndexNumber)).
- Included after the reference to the Charts collection (Charts(IndexNumber)).
- The Chart object’s name (Chart.Name property). The name is:
- Wrapped in:
- Double quotes (“ChartSheetName”); and
- Parentheses ((“ChartSheetName”)).
- Included after the reference to the Charts collection (Charts(“ChartSheetName”)).
- Wrapped in:
- An index number. The index number:
- You can (also) use a Chart object’s CodeName property (Chart.CodeName property) to refer to the applicable chart sheet.
Macro Example to Activate Workbook and Chart Sheet
The macro below does the following:
- Activate:
- The “Excel VBA Activate Workbook.xlsm” workbook; and
- The “Activate Workbook Chart Sheet” chart sheet.
- Maximize the active window.
Sub ActivateWorkbookAndChartSheet() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'This procedure: '(1) Activates the "Activate Workbook Chart Sheet" chart sheet in the "Excel VBA Activate Workbook.xlsm" workbook '(2) Maximizes the active window 'Activate the "Activate Workbook Chart Sheet" chart sheet in the "Excel VBA Activate Workbook.xlsm" workbook Workbooks("Excel VBA Activate Workbook.xlsm").Charts("Activate Workbook Chart Sheet").Activate 'Maximize the active window ActiveWindow.WindowState = xlMaximized End Sub
Effects of Executing Macro Example to Activate Workbook and Chart Sheet
The image below illustrates the effects of using the macro example. In this example:
- 2 workbooks (“Excel VBA Activate Workbook.xlsm” and “Book1”) are open.
- The “Book1” workbook is the active workbook.
- The “Sheet1” worksheet is the active sheet in both (“Excel VBA Activate Workbook.xlsm” and “Book1”) workbooks.
When the macro is executed, Excel:
- Activates:
- The “Excel VBA Activate Workbook.xlsm” workbook; and
- The “Activate Workbook Chart Sheet” chart sheet.
- Maximizes the active window.
Excel VBA Activate Workbook with Variable Name
VBA Code to Activate Workbook with Variable Name
To activate a workbook with variable name (where the workbook filename is held by a variable), use the following structure/template in the applicable procedure.
VariableDeclarationStatement WorkbookFilenameVariable As String WorkbookFilenameVariable = WorkbookFilenameString Workbooks(WorkbookFilenameVariable).Activate
Process to Activate Workbook with Variable Name
- Declare variable (usually of the String data type) to represent workbook filename.
- Assign workbook filename to variable.
- Refer to the workbook to activate by using the applicable variable name (representing the workbook filename).
- Activate the applicable workbook.
Main VBA Constructs Used to Activate Workbook with Variable Name
(1) Variable declaration statement.
One of the following 4 statements:
- Dim: Declares variables and allocates storage space.
- Private:
- Used at the module level.
- Declares module-level variables and allocates storage space.
- Public:
- Used at the module level.
- Declares global variables and allocates storage space.
- Static: Declares static variables and allocates storage space.
(2) String data type.
Holds textual data (a sequence of characters representing the characters themselves).
(3) Assignment operator (=).
Assigns a value to a variable or property.
(4) Application.Workbooks property.
Returns a Workbooks collection representing all open workbooks.
(5) Workbooks object.
Represents all open workbooks.
(6) Workbooks.Item property.
Returns a specific Workbook object from the applicable Workbooks collection.
(7) Workbook object.
Represents an Excel workbook.
(8) Variable.
A named storage location containing data that can be modified during procedure execution.
(9) Workbook.Activate method.
Activates the (first) window associated with a workbook.
Cues to Activate Workbook with Variable Name with VBA
- Consider explicitly declaring:
- The variable representing the workbook filename.
- The data type (usually String) of the variable representing the workbook filename.
- As a general rule, declare the variable representing the workbook filename:
- Using the Dim statement; and
- As of the String data type.
- If the scope of the variable representing the workbook filename is module-level or global, follow the applicable rules for (module-level or global) variable declaration.
- When assigning the workbook filename to the variable representing the workbook filename:
- Consider whether you must wrap the workbook filename in double quotes and parentheses ((“Filename”)).
- As a general rule:
- Include the complete filename, including file extension (for ex., “Filename.xlsm”), of a previously saved workbook.
- Do not include the file path.
Macro Example to Activate Workbook with Variable Name
The macro below does the following:
- Declare a variable (WorkbookFilename) of the String data type.
- Assign a string (Excel VBA Activate Workbook.xlsm) to the WorkbookFilename variable.
- Activate the workbook whose filename is represented by the WorkbookFilename variable.
- Maximize the active window.
Sub ActivateWorkbookWithVariableName() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'This procedure: '(1) Declares a variable (WorkbookFilename) of the String data type '(2) Assigns a string (Excel VBA Activate Workbook.xlsm) to the WorkbookFilename variable '(3) Activates the workbook whose filename is represented by the WorkbookFilename variable '(4) Maximizes the active window 'Declare variable to represent workbook filename Dim WorkbookFilename As String 'Assign workbook filename to variable WorkbookFilename = "Excel VBA Activate Workbook.xlsm" 'Activate the workbook whose filename is represented by the WorkbookFilename variable Workbooks(WorkbookFilename).Activate 'Maximize the active window ActiveWindow.WindowState = xlMaximized End Sub
Effects of Executing Macro Example to Activate Workbook with Variable Name
The image below illustrates the effects of using the macro example. In this example:
- 2 workbooks (“Excel VBA Activate Workbook.xlsm” and “Book1”) are open.
- The “Book1” workbook is the active workbook.
When the macro is executed, Excel:
- Activates the workbook whose filename (Excel VBA Activate Workbook.xlsm) is held by a variable (WorkbookFilename); and
- Maximizes the active window.
Excel VBA Activate Workbook with Object Variable Name
VBA Code to Activate Workbook with Object Variable Name
To activate a workbook with object variable name (where the Workbook object is represented by an object variable), use the following structure/template in the applicable procedure.
VariableDeclarationStatement WorkbookObjectVariable As Workbook Set WorkbookObjectVariable = WorkbookObjectReference WorkbookObjectVariable.Activate
Process to Activate Workbook with Object Variable Name
- Declare object variable (usually of the Workbook object data type) to represent workbook.
- Assign Workbook object reference to object variable.
- Refer to the workbook to activate by using the applicable object variable name (representing the workbook).
- Activate the applicable workbook.
Main VBA Constructs Used to Activate Workbook with Object Variable Name
(1) Variable declaration statement.
One of the following 4 statements:
- Dim: Declares variables and allocates storage space.
- Private:
- Used at the module level.
- Declares module-level variables and allocates storage space.
- Public:
- Used at the module level.
- Declares global variables and allocates storage space.
- Static: Declares static variables and allocates storage space.
(2) Workbook object.
Represents an Excel workbook.
(3) Set statement.
Assigns an object reference to a variable or property.
(4) Application.Workbooks property.
Returns a Workbooks collection representing all open workbooks.
(5) Workbooks object.
Represents all open workbooks.
(6) Workbooks.Item property.
Returns a specific Workbook object from the applicable Workbooks collection.
(7) Object Variable.
A named storage location containing data (a reference to an object) that can be modified during procedure execution.
(8) Workbook.Activate method.
Activates the (first) window associated with a workbook.
Cues to Activate Workbook with Object Variable Name with VBA
- Consider explicitly declaring:
- The object variable representing the workbook.
- The data type (usually Workbook object) of the object variable representing the workbook.
- As a general rule, declare the object variable representing the workbook:
- Using the Dim statement; and
- As of the Workbook object data type.
- If the scope of the object variable representing the workbook is module-level or global, follow the applicable rules for (module-level or global) variable declaration.
Macro Example to Activate Workbook with Object Variable Name
The macro below does the following:
- Declare an object variable (WorkbookObjectVariable) of the Workbook object data type.
- Assign a Workbook object reference (Excel VBA Activate Workbook.xlsm) to the WorkbookObjectVariable object variable.
- Activate the workbook represented by the WorkbookObjectVariable object variable.
- Maximize the active window.
Sub ActivateWorkbookWithObjectVariableName() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'This procedure: '(1) Declares an object variable (WorkbookObjectVariable) of the Workbook object data type '(2) Assigns a Workbook object reference (to the "Excel VBA Activate Workbook.xlsm" workbook) to the WorkbookObjectVariable object variable '(3) Activates the workbook represented by the WorkbookObjectVariable object variable '(4) Maximizes the active window 'Declare object variable to represent workbook Dim WorkbookObjectVariable As Workbook 'Assign Workbook object reference to object variable Set WorkbookObjectVariable = Workbooks("Excel VBA Activate Workbook.xlsm") 'Activate workbook represented by the WorkbookObjectVariable object variable WorkbookObjectVariable.Activate 'Maximize the active window ActiveWindow.WindowState = xlMaximized End Sub
Effects of Executing Macro Example to Activate Workbook with Object Variable Name
The image below illustrates the effects of using the macro example. In this example:
- 2 workbooks (“Excel VBA Activate Workbook.xlsm” and “Book1”) are open.
- The “Book1” workbook is the active workbook.
When the macro is executed, Excel:
- Activates the workbook whose reference (Excel VBA Activate Workbook.xlsm) is held by an object variable (WorkbookObjectVariable); and
- Maximizes the active window.
Excel VBA Open and Activate Workbook
VBA Code to Open and Activate Workbook
To open and activate a workbook, use the following structure/template in the applicable statement.
Workbooks.Open Filename:="PathAndFilename"
Process to Open and Activate Workbook with VBA
- Open the workbook with the Workbooks.Open method.
Main VBA Constructs Used to Open and Activate Workbook
(1) Workbooks.Open method.
Opens a workbook.
(2) Filename parameter.
The:
- First parameter of the Workbooks.Open method.
- Filename of the workbook to open.
Cues to Open and Activate Workbook with VBA
- When you open a workbook with the Workbooks.Open method, the opened workbook becomes the active workbook.
- Consider whether you must wrap the workbook path and filename in double quotes (“PathAndFilename”).
- As a general rule:
- Include the full path and filename (including file extension).
- If the workbook is stored in the default file folder (where Excel currently saves files by default), you can exclude the workbook’s path (use only the filename, including file extension).
Macro Example to Open and Activate Workbook
The macro below opens (and activates) the workbook with filename “Excel VBA Open and Activate Workbook.xlsx” stored in the Desktop folder.
Sub OpenAndActivateWorkbook() 'Source: https://powerspreadsheets.com/ 'For further information: https://powerspreadsheets.com/excel-vba-activate-workbook/ 'Open the workbook with filename "Excel VBA Open and Activate Workbook.xlsx" stored in the Desktop folder. The opened workbook becomes the active workbook Workbooks.Open Filename:="C:UsersPowerSpreadsheetsDesktopExcel VBA Open and Activate Workbook.xlsx" End Sub
Effects of Executing Macro Example to Open and Activate Workbook
The image below illustrates the effects of using the macro example.
In this example (only) 1 workbook (“Excel VBA Activate Workbook”) is open. This is (also) the active workbook.
When the macro is executed, Excel opens and activates the workbook with filename “Excel VBA Open and Activate Workbook.xlsx” stored in the Desktop folder.