VBA Activate Workbook in Excel method 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.
Here is the following syntax to activate Workbook in Excel VBA using activate method.
Where activate is a method.
Let us see the following example. It activate Workbook with Workbook Name in Excel VBA. In the below example ‘sWorkbook1’ and ‘sWorkbook2’ represent workbook names. First we are trying to activate workbook ‘sWorkbook1’ and then activating workbook ‘sWorkbook2’ file. Activate method helps to activate workbook.
'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 'you can change two Workbook file paths here sWorkbook1 = "D:\VBAF1\WB1.xlsm" sWorkbook2 = "D:\VBAF1\WB2.xlsm" 'Activate first Workbook Workbooks(sWorkbook1).Activate 'Activate second Workbook Workbooks(sWorkbook2).Activate End Sub
Here is the following example. Before running below macro procedure three workbooks should be open. Then only we can notice how the workbooks are active based on index number. VBA Code to Activate Workbook with Workbook Index Number in Excel VBA. In the below example Workbooks(1) represents the first workbook. Similarly Workbooks(3) indicates the third workbook. where numbers 1 and 3 are index numbers of workbook.
'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
You can refer the following link for the step by step instructions to execute VBA procedure or code.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.