Description:

Sometimes we may want to Activate Workbook Or Worksheet using Excel VBA.You can use Activate method in VBA to Select the required Workbook Or WorkSheet using VBA and perform required tasks.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

BROWSE ALL TEMPLATES

50+ Project Management Templates Pack
Excel PowerPoint Word

VIEW DETAILS

Advanced Project Plan & Portfolio Template
Excel Template

VIEW DETAILS

Ultimate Project Management Template
Excel Template

VIEW DETAILS

20+ Excel Project Management Pack
Excel Templates

VIEW DETAILS

20+ PowerPoint Project Management Pack
PowerPoint Templates

VIEW DETAILS

10+ MS Word Project Management Pack
Word Templates

VIEW DETAILS


For example, we may have 5 worksheets, 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 WorkSheet with VBA- Example Cases:

VBA Activate Workbook or Worksheet

If you know the name of the workbook, you can use the following VBA code to Activate Workbook or worksheet. For example workbook name is “Book2” which we want to activate, it contains sheet named as “Sheet1”. Using following code you can Choose the required Workbook and Sheet, and Activate Workbook Or Work Sheet using Excel VBA.

Code:

Sub Activate_Workbook()
    
    'Activate Workbook
    Workbooks("Book2.xls").Activate   

    'Activate Worksheet
    Workbooks("Book2.xls").Sheets("Sheet1").Activate

End Sub

Output:

VBA Activate Workbook Or Work Sheet

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a new module from Insert menu
  4. Copy the above code and Paste in the code window
  5. Press F5 to see the output
  6. You should see Aactivated workbook and Worksheet
  7. 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. In this approach, you will assign the Workbook to any object and select Workbook with VBA to perform required operations using Excel Macros.

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:
  1. We declared two objects for workbook and worksheet
  2. We have added a new workbook and assigned to workbook object
  3. We set the Worksheet to worksheet object
  4. We activated the worksheet by refering the worksheet object
  5. .

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a new module from Insert menu
  4. Copy the above code and Paste in the code window
  5. Press F5 to see the output
  6. You should see output as shown above
  7. Save the file as macro enabled workbook
VBA to Select Workbook or Worksheet in Excel

When we have multiple worksheets in a workbook, we can select required sheet using VBA. We can also select required workbook when we have more than one Excel Workbook is opened. It is good practice to set the required workbook or sheet to an object. And refer it whenever it is required. This helps you to accessing workbook or sheet without selecting or activating it.

Code:

Sub Activate_Workbook_Using_Object()
    
    'Variable Declaration
    Dim wbAs Workbook
    Dim wsAs Worksheet
    
    'Select any Workbook and assign to an Object
    Set wb= Workbooks("Book1.xlsm")
    
    'Create Object for Worksheet
    Set ws= wb.Sheets("SheetName")
    
  'VBA to Select or Activate the required Workbook
    wb.Activate

    'VBA to Select or Activate the required Sheet
    ws.Activate

End Sub

LIMITED TIME OFFER - Get it Now!
Advanced Project Plan Excel Template

 
Related Resource External VBA Reference