Description:
Sometimes we may want to Activate Workbook Or Worksheet using Excel VBA.You can use Activate property Activate Workbook Or Work Sheet.
ON SALE80% OFF
50+ Project Management Templates Pack
Excel PowerPoint Word
Advanced Project Plan & Portfolio Template
Excel Template
Ultimate Project Management Template
Excel Template
20+ Excel Project Management Pack
Excel Templates
20+ PowerPoint Project Management Pack
PowerPoint Templates
10+ MS Word Project Management Pack
Word Templates
For example, we may have 5 worksheet, we have to do some tasks in Sheet3. In order to perform some tasks we need to activate the worksheet then perform the tasks.
Activate Workbook Or Work Sheet – Example Cases:
- You may want to Activate Workbook Or Work Sheet
- You may want to Activate Workbook Or Work Sheet using object
Activate Workbook or Worksheet
If you know the name of the workbook You can use the following code to Activate Workbook or worksheet. For example workbook name is “Book2” which we want to activate and it contains sheet named as “Sheet1”. Using following code you can Activate Workbook Or Work Sheet.
Code:
Sub Activate_Workbook() 'Activate Workbook Workbooks("Book2.xls").Activate 'Activate Worksheet Workbooks("Book2.xls").Sheets("Sheet1").Activate End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Press F5 to see the output
- You should see Aactivated workbook and Worksheet
- Save the file as macro enabled workbook
Activate Workbook or Worksheet using object
You can create object for workbook and worksheet in the following way.You can use object to activate Workbook or Worksheet.
Code:
Sub Activate_Workbook_Using_Object() 'Variable Declaration Dim WrkBk As Workbook Dim WrkSht As Worksheet 'Create Object for Workbook Set WrkBk = Workbooks.Add 'Create Object for Worksheet Set WrkSht = WrkBk.Sheets("Sheet1") 'Activate Sheet WrkSht.Activate End Sub
Explanation:
- We declared two objects for workbook and worksheet
- We have added a new workbook and assigned to workbook object
- We set the Worksheet to worksheet object
- We activated the worksheet by refering the worksheet object
.
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a new module from Insert menu
- Copy the above code and Paste in the code window
- Press F5 to see the output
- You should see output as shown above
- Save the file as macro enabled workbook
Related Resource
Excel VBA Reference | Project Management Reference |
---|---|
VBA Reference: Excel Reference: |
Hi,
If you worksheet has a space in its name eg: ‘Sheet 1’, how would you reference that?
(Workbooks(“Book2.xls”).Sheets(“Sheet1”).Activate)
Thanks-PNRao!
Hey Valli,
I have just got a very simple question.
My current macro pastes into a specific file Windows(“test file.xls”).Activate with this code. I would like to use this macro on various files with different names. Therefore I want it to paste onto my active worksheet, and am unsure of what code it should be .
Please help!
Regards