Wauvel

Excel like a finance pro.

← The library

VBA · Objects

ThisWorkbook vs ActiveWorkbook

ThisWorkbook is the file the code lives in; ActiveWorkbook is whichever one is in front.

Very commonDifficulty 1150 · Proficient
Practice · 2 questions →

When to use it

ThisWorkbook is the workbook the code lives in. ActiveWorkbook is whatever is in front, which changes when the user clicks. Code that means "my file" should say ThisWorkbook.

The code

Code
ThisWorkbook.Worksheets("Data").Range("A1").Value = 1

Worked examples

  • Explicit

    ThisWorkbook.Worksheets("Data").Range("A1").Value = 1 Writes to this file regardless of what is active

    Safe.

  • Ambiguous

    ActiveWorkbook.Save Saves whatever is in front, maybe not your file

    Risky.

  • Another workbook

    Set wb = Workbooks.Open("C:\data\input.xlsx") wb.Worksheets(1).Range("A1").Copy A variable for another workbook

    Then both are explicit.

Worth knowing

  • Prefer ThisWorkbook and explicit sheet names over Active anything.
  • Assign opened workbooks to variables and stop using Active anything.
  • ThisWorkbook.Path gives the folder the file is in.
  • In an add-in, ThisWorkbook is the add-in, not the user's file.

Where it goes wrong

  • ActiveWorkbook after Workbooks.Open is the opened file, which is fine until another opens.
  • Unqualified Worksheets("Data") means the active workbook.

Related

Learn the moves here — or let Wauvel run them on your numbers.

Meet your AI CFO →

One CFO-grade Excel tip a week

A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.