VBA Activate Workbook in Excel

Activate Workbook method in Excel VBA helps to deal with multiple workbooks. It helps to activate specified Workbook window first. Sometimes we may need to activate specific workbook. We can activate specific workbook with Workbook Name or Index number.

VBA Activate Workbook

Syntax for Activate Workbook Method

Here is the following syntax to activate Workbook in Excel VBA using activate method.

Workbooks.Activate

Macro to Activate Workbook with Workbook Name in Excel VBA

Let us see the following example. It activate Workbook with Workbook Name in Excel VBA.

'Activate Workbook with Workbook Name in Excel VBA
Sub VBA_Activate_Workbook_with_Workbook_Name()

    'Variable declaration
    Dim sWorkbook1 As String
    Dim sWorkbook2 As String
    
    sWorkbook1 = "D:\VBAF1\WB1.xlsm"
    sWorkbook2 = "D:\VBAF1\WB2.xlsm"
    
    'Activate first Workbook
    Workbooks(sWorkbook1).Activate
    
    'Activate second Workbook
    Workbooks(sWorkbook2).Activate
        
End Sub

VBA Code to Activate Workbook with Workbook Index Number in Excel VBA

Here is the following example. VBA Code to Activate Workbook with Workbook Index Number in Excel VBA.

'Activate Workbook by Index Number in Excel VBA
Sub VBA_Activate_Workbook_by_Index_Number()
    
    'Activate first Workbook
    Workbooks(1).Activate
    
    'Activate Third Workbook
    Workbooks(3).Activate
     
End Sub

Instructions to use Macro

Here are the instructions to use above macro procedure in Visual basic editor.

  • Open Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • You can see output on the screen
  • Find above output screenshot of the specified procedure.

Related Workbook Object articles

You may also like the related Workbook Object articles.

VBA Workbook Object






Leave a Comment

Your email address will not be published. Required fields are marked *