VBA Difference Between ActiveWorkbook ThisWorkbook

VBA Difference Between ActiveWorkbook ThisWorkbook In Excel

VBA Difference Between ActiveWorkbook ThisWorkbook In Excel. ThisWorkbook represents the current workbook in which the code is available. ActiveWorkbook represents the currently active workbook from multiple different open Workbooks.

Difference Between ActiveWorkbook and ThisWorkbook in EXcel VBA

Let us see the VBA Difference Between ActiveWorkbook and ThisWorkbook.

ActiveWorkbook ThisWorkbook
ActiveWorkbook represents the Workbook in active window which has focus on the screen. ThisWorkbook represents the current Workbook in which the current VBA code is running or executing.
The Workbook window is active / visible. The Workbook window is Inactive / Invisible.
Get Active Workbook Name:
ActiveWorkbook.Name
Get Current Workbook Name:
ThisWorkbook.Name
Activate ActiveWorkbook:
ActiveWorkbook .Activate
Activate ThisWorkbook:
ThisWorkbook .Activate
Example:

'VBA Get Active Workbook Name
Sub VBA_Get_ActiveWorkbook_Name()
    
    'Variable declaration
    Dim sActiveWorkbookName As String
    
    sActiveWorkbookName = ActiveWorkbook.Name
    
End Sub
Example:

'VBA Get Current Workbook Name
Sub VBA_Get_CurrentWorkbook_Name()
    
    'Variable declaration
    Dim sCurrentWorkbookName As String
    
    sCurrentWorkbookName = ThisWorkbook.Name
    
End Sub

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply