VBA & Macro Problems

Fix VBA Error 9 When Activating a Workbook in WPS Office Excel

Emma BrownEmma Brown Nov 13, 2026 869 views

Question details

The user needs to resolve VBA Run-time Error 9, 'Subscript out of range,' when a macro cannot find a workbook or window by name while activating an Excel workbook in WPS Office Excel.

Fix VBA Error 9 When Activating a Workbook in WPS Office Excel
Product
WPS Office Excel (VBA/Macro)
Device & OS
not provided
Scenario
A VBA macro attempts to activate a specific open workbook or workbook window by name.
Observed behavior
The macro stops with VBA Run-time Error 9 'Subscript out of range' because the specified workbook or window cannot be found in the Workbooks or Windows collection.
Before you start

Before you edit the macro, open the target workbook in WPS Office Excel and confirm the exact file name shown in the title bar, including the .xlsx or .xlsm extension.

Solution 1Recommended

Use ThisWorkbook.Activate When the Macro and Target Workbook Are the Same

Use this when the macro is stored in the workbook that should be activated; ThisWorkbook always refers to the workbook containing the running VBA code.

This avoids name mismatches and extension issues because ThisWorkbook automatically points to the workbook where the VBA code runs.

1
Open the VBA editor

In WPS Office Excel, press Alt+F11 to open the Microsoft Visual Basic for Applications editor.

2
Locate the macro module

In the Project Explorer, find the module or worksheet code containing the macro that calls Activate.

3
Replace the workbook reference with ThisWorkbook

Change the activation line to ThisWorkbook.Activate instead of Workbooks("A3 Daily Ups and Downs.xlsm").Activate.

4
Run the macro

Press F5 or select Run > Run Sub/UserForm to execute the macro and confirm the workbook is activated without the error.

Use ThisWorkbook.Activate When the Macro and Target Workbook Are the Same
Best for macros stored in the target workbook: ThisWorkbook does not require the file name or extension, so it avoids 'Subscript out of range' caused by name mismatches.

Fix Workbook Activation Macros in WPS Office Excel

Use WPS Office Excel to run and edit VBA macros reliably. With the same Workbooks and Windows collections as Microsoft Excel, you can check exact workbook names, use ThisWorkbook.Activate, and add error handling to prevent Run-time Error 9.

  1. 1. Open your macro-enabled workbook: Launch WPS Office Excel and open the .xlsx or .xlsm workbook that contains the VBA macro.
  2. 2. Open the VBA editor: Press Alt+F11 to open the VBA editor and locate the macro that produces Error 9.
  3. 3. Check the workbook reference: Replace the failing reference with ThisWorkbook.Activate or set a Workbook variable after verifying the exact file name in Workbooks("...").
  4. 4. Run and test the macro: Press F5 to run the macro and confirm that the workbook is activated without the 'Subscript out of range' error.
Runs VBA macros with familiar Workbooks, Windows, and ThisWorkbook objects for Excel-compatible automation.Opens and saves .xlsx and .xlsm workbooks without converting file formats.Free, lightweight Office suite that keeps your existing macro-enabled workflows working.No need to change the VBA code structure when migrating from Microsoft Excel.
microsoft office alternative - wps office

Frequently Asked Questions

Why does VBA show "Subscript out of range" when activating a workbook?

This error usually appears when the exact workbook name or window name is not found in the Workbooks or Windows collection, or when the workbook is not open. Check the title bar name, extension, and open status.

Can I use a full file path in Workbooks?

No, a full file path cannot normally be used as the key in the Workbooks collection. Open the file first with Workbooks.Open using the full path, then activate the workbook by its file name.

When should I use ThisWorkbook instead of Workbooks("name")?

Use ThisWorkbook when the macro is stored in the workbook you want to activate. It refers to the workbook containing the running VBA code and avoids name and extension mismatches.

What is the difference between Workbooks and Windows in VBA?

The Workbooks collection contains open workbooks, while the Windows collection contains workbook windows. If you reference a window name but use Workbooks, or the reverse, Run-time Error 9 can occur.