Macro Monday – VBA ActiveWorkbook vs ThisWorkbook


Hello Excellers and welcome back to another blog post in my #macromondays series.  Today I want to go back to Excel Macro basics for a short time and discuss the fundamental and important differences between ActiveWorkbooks and ThisWorkbooks.

At the most basic level….

ActiveWorkbook – this is the workbook that is the topmost Excel window or the one that has the current focus.

ThisWorkbook – this is the workbook in which the macro is running.

 

Why Is This Important To Know?

It is vital to understand the difference between the difference between these two workbooks.  It is doubly important not to mix them up if you are working in multiple workbooks at the same time.  In particular, if you run a macro from one workbook accidentally when another workbook is currently active.  If you are referring to or using a VBA Range or VBA Cell directly, then this can have far-reaching and disastrous effects.

 

Explaining The Difference ActiveWorkbook vs ThisWorkbook

The active workbook refers to the workbook that is currently in focus and not the workbook that is visible to the user.  Users can have multiple workbooks open and display them side by side on the screen.  The active workbook is the only one that is currently selected.  users can only have one workbook selected and that is the ActiveWorkbook.  Below is a direct explanation from Microsoft.

[stextbox id=’info’]

Application.ActiveWorkbook or ActiveWorkbook in Excel VBA

Returns a Workbook object that represents the workbook in the active window (the window on top). Read-only. Returns Nothing if there are no windows open or if either the Info window or the Clipboard window is the active window.

[/stextbox]

 

In contrast, ThisWorkbook simply refers to the workbook that the VBA code is actually executing.  Below is a direct explanation from Microsoft.

[stextbox id=’info’]

Application.ThisWorkbook or ThisWorkbook in Excel VBA

The ThisWorkbook property returns the workbook where the Visual Basic code is running.

[/stextbox]

 

 

Do You Now Know The Difference?

 

 

 

What Next? Want More Tips?

So, if you want more tips then sign up for my Monthly Newsletter where I share 3 Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.

1If you want to see all of the blog posts in the Macro Monday series. Click on the link below

How To Excel At Excel – Macro Mondays Blog Posts.

 

Learn Excel With Our Excel Online Courses

Do You Need Help With An Excel Problem?.

Finally, I am pleased to announce I have teamed up with Excel Rescue, where you can get help FAST.  All you need to do is choose the Excel task that most closely describes what you need to be done.  Above all, there is a money back guarantee and similarly Security and Non-Disclosure Agreements.  Try It!.  Need Help With An Excel VBA Macro?.  Of course, you don’t need to ask how to list all files in a directory as it is right here for free.

 

ExcelRescue.net

Udemy.com Home page 125x125

Barbara

Barbara is an experienced data analyst with over 20 years of experience. Author of the How To Excel At Excel Top 50 Excel tips Ebook, YouTuber and Excel Blogger.

Recent Posts