logo
search
VBA & Macro Problems

How to Use ActiveSheet Variables Correctly in Excel VBA

Adam DavisAdam Davis Oct 13, 2026 868 views

Question details

The user needs to ensure their VBA macro prints or modifies the intended original worksheet after opening a new workbook.

How to Use ActiveSheet Variables Correctly in Excel VBA
Product
Excel
Device & OS
not provided
Scenario
A VBA procedure stores a worksheet reference, subsequently opens a new workbook, and then attempts to execute a print command.
Observed behavior
The macro mistakenly prints the newly active workbook because it relies on ActiveWindow.SelectedSheets instead of the previously stored worksheet variable.
Before you start

Open your Visual Basic Editor (ALT + F11) and locate the specific macro where the worksheet reference is assigned and the PrintOut command is executed.

Solution 1Recommended

Use Fully Qualified Worksheet References

Replace implicit references like ActiveWindow.SelectedSheets with the specific worksheet variable you originally declared.

When you use 'Set WS = ActiveSheet', the variable 'WS' holds a permanent reference to that specific sheet. However, if you subsequently open a new workbook, that new workbook becomes the active window.

Using commands like 'ActiveWindow.SelectedSheets.PrintOut' or unqualified 'Range' objects will target the new workbook instead of your original sheet. To fix this, you must explicitly prefix your commands with your stored variable.

1
Locate the variable declaration

Find the Worksheet variable declaration in your VBA code (e.g., Dim WS As Worksheet).

2
Assign the worksheet

Ensure the original worksheet is assigned to the variable before opening the new workbook (e.g., Set WS = ActiveSheet).

3
Identify the flawed command

Find the execution command causing the issue, such as 'ActiveWindow.SelectedSheets.PrintOut'.

4
Apply a fully qualified reference

Replace the implicit command with a fully qualified reference using your variable, such as 'WS.PrintOut'.

5
Update ranges and cells

Apply this practice to all ranges in the procedure by prefixing them with the variable (e.g., use 'WS.Range("A1")' instead of just 'Range("A1")').

Use Fully Qualified Worksheet References
Best Practice: Always explicitly state the worksheet variable before ranges, cells, or commands to prevent VBA from defaulting to the currently active window.
Advanced Macro Editor

Execute and Edit VBA Macros Seamlessly in WPS Office

WPS Spreadsheet provides a built-in macro editor that fully supports standard VBA syntax, allowing you to easily manage, debug, and execute your macros with fully qualified worksheet references.

  1. 1. Open Workbook: Open your macro-enabled workbook in WPS Spreadsheet.
  2. 2. Access Tools Tab: Navigate to the 'Tools' tab on the top ribbon menu.
  3. 3. Open Visual Basic Editor: Click on 'Macros' and select 'Visual Basic Editor' (or press ALT + F11).
  4. 4. Edit Code: Edit your ActiveSheet variables and fully qualified references directly in the WPS code window.
Fully compatible with Microsoft Excel VBA syntax and .xlsm formatsBuilt-in Visual Basic Editor to correctly debug ActiveSheet variablesLightweight, fast, and completely free to download
QA img-9

Frequently Asked Questions

Why does my VBA macro print the wrong workbook?

If your macro opens a new workbook, that new workbook becomes the active window. Any commands relying on ActiveWindow or ActiveSheet will then target the new workbook instead of your original one.

Does the ActiveSheet variable update automatically when a new sheet is opened?

No. A variable assigned using 'Set WS = ActiveSheet' permanently points to the specific worksheet that was active at the exact moment the Set command was executed, regardless of what becomes active later.

How do I refer to a cell within my stored worksheet variable?

You must prefix the Range or Cells command with your variable name. For example, you should write 'WS.Range("A1")' instead of simply writing 'Range("A1")'.