[Excel VBA] The Decisive Difference Between ThisWorkbook and ActiveWorkbook and How to Use Them Correctly

目次

Introduction

When you start writing code in Excel VBA, you often encounter two keywords: ThisWorkbook and ActiveWorkbook.

While both appear to refer to an Excel workbook, their meanings are entirely different. Failing to understand this distinction can lead to macros operating on the wrong file, causing errors or unintended data changes.

This article explains the critical difference between these two fundamental VBA keywords and provides guidelines on when to use which.

What is ThisWorkbook? – The Workbook Where the Code Lives

ThisWorkbook always refers to the specific Excel file where the VBA code is currently written and saved.

This is an immutable reference. Even if the user opens other Excel files or switches the active window, the workbook that ThisWorkbook points to never changes.

Analogy: ThisWorkbook is like your “Home.” No matter where you go or what you look at, the location of your “Home” remains the same.

Code Example

This code will always operate on “Sheet1” cell A1 of the workbook containing this macro, regardless of which workbook is currently active.

Sub ManipulateThisWorkbook()
    ' Writes value to Cell A1 of "Sheet1" in the workbook where this macro is saved
    ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = "This is the Macro book."
    
    ' Always operates on this macro's workbook, regardless of the active window
    MsgBox "Operated on cells in: " & ThisWorkbook.Name
End Sub

What is ActiveWorkbook? – The Currently Active Workbook

ActiveWorkbook refers to the workbook that is currently displayed at the front of the Excel screen (the one with the focus).

This is a variable reference. If the user clicks on a different workbook window to make it active, the target of ActiveWorkbook changes instantly.

Analogy: ActiveWorkbook is like the “View” you are currently looking at. If you turn your head (switch windows), the “View” changes.

Code Example

If you open multiple Excel workbooks and switch between them before running this macro, you will see that the result changes.

Sub ShowActiveWorkbookName()
    ' Displays the name of the workbook currently at the front
    ' Try clicking another workbook to make it active before running this
    If ActiveWorkbook Is Nothing Then
        MsgBox "No active workbook."
    Else
        MsgBox "The currently active workbook is: " & ActiveWorkbook.Name
    End If
End Sub

Correct Usage and Precautions

Distinguishing between these two is crucial for macro stability.

When to Use ThisWorkbook

  • When reading from or writing to sheets contained within the macro file itself (e.g., Settings sheets, Data Masters, Log sheets).
  • When you want to operate on a specific workbook regardless of user actions.

Conclusion: Operating within the macro’s own workbook is the standard approach. When in doubt, use ThisWorkbook.

When to Use ActiveWorkbook

  • When creating generic tools (like Add-ins) that need to process any file the user currently has open.
  • When the explicit purpose of the macro is “to do something to the file the user is currently looking at.”

Conclusion: Use this only when you intentionally want to manipulate the user’s active file.

Recommendation: To prevent the target from changing midway through processing, assign it to a variable at the start of your code: Set myBook = ActiveWorkbook.

Summary

The differences between ThisWorkbook and ActiveWorkbook can be summarized as follows:

KeywordTargetFeature
ThisWorkbookThe workbook where the code is written.Immutable / Stable. Not affected by user actions.
ActiveWorkbookThe currently active workbook.Mutable / Dynamic. Switches based on user actions.

The ironclad rule for creating stable VBA macros is: “Base your logic on ThisWorkbook as much as possible, and limit the use of ActiveWorkbook to intentional scenarios.” Mastering this distinction is a major step away from being a beginner.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次