Excel VBA Workbook Select: How to Activate Workbooks
In this article
To select a workbook in Excel VBA, use the Workbooks collection: Workbooks("Book2.xlsx").Activate brings that workbook to the front, while Workbooks("Book2.xlsx").Worksheets(1).Select also selects a sheet inside it. There is no Workbooks.Select method — Select exists on worksheets, ranges and shapes, not on workbooks, which is why Activate is the correct verb for the workbook object. The steps below cover the working syntax, how to reference workbooks by name, index or variable, and the errors that trip people up most often.
What You'll Need
- Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac). Labels can differ slightly by version and region.
- At least two open workbooks if you want to see switching happen — one to hold the code and one to be activated.
- Macro-enabled file format (.xlsm) if you plan to save the code.
- Basic familiarity with the Visual Basic Editor (Alt+F11 on Windows, Option+F11 on most Mac layouts).
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). If nothing happens, enable the Developer tab first and use Developer > Visual Basic.
- Insert a module. In the Project Explorer, right-click your workbook's VBAProject and choose Insert > Module. Code in a standard module can be run with Run > Run Sub or F5.
- Write the activation line using the workbook name. Type
Sub SwitchWorkbook(), press Enter, then on the next line typeWorkbooks("Book2.xlsx").Activate. The name must include the file extension exactly as Excel shows it in the title bar. - Close the procedure. Add
End Subon its own line. Your code should now read:Sub SwitchWorkbook()Workbooks("Book2.xlsx").ActivateEnd Sub - Run it. Place the cursor inside the procedure and press F5. Excel switches to the named workbook. If the workbook is minimised,
Activaterestores it, which is one reason it is preferred over settingActiveWorkbookindirectly. - Reference by index instead of name. If you do not want to hard-code a filename, use
Workbooks(2).Activate. Index numbers follow the order workbooks were opened, so this is fragile — a workbook opened earlier shifts every number. Use it only for quick tests. - Use a variable for repeated switching. Declare
Dim wb As Workbook, thenSet wb = Workbooks("Book2.xlsx")and laterwb.Activate. This is faster to read and avoids retyping the filename; it also lets you test withIf Not wb Is Nothingbefore acting. - Activate a workbook and a sheet together. Activating a workbook does not change which sheet is active inside it. To land on a specific sheet, follow with
wb.Worksheets("Data").Activate. UseActivateon the sheet rather thanSelectunless you specifically need a multi-sheet selection. - Select a range after switching. Once the workbook and sheet are active,
Range("A1:D10").Selectworks. Selecting a range on a sheet that is not active raises run-time error 1004, so the order matters: workbook, then sheet, then range. - Handle a missing workbook gracefully. Wrap the call in error handling so a renamed or closed file does not halt the macro:
On Error Resume Next, theActivateline, thenIf Err.Number <> 0 Then MsgBox "Workbook not open"andOn Error GoTo 0. Better still, check with a loop overWorkbookscomparing.Name. - Save and test with the target workbook closed. Close Book2 and run the macro again. You should get the friendly message rather than a debug prompt — proof the error handling works before you rely on it.
Common Mistakes
- Writing
Workbooks("Book2").Select. Workbooks have noSelectmethod, and the name is missing its extension. Both faults produce errors or silent failures. - Confusing
ActiveWorkbookwithThisWorkbook.ThisWorkbookis always the file containing the code;ActiveWorkbookis whichever window has focus. AfterActivate, they may differ, and code that assumes otherwise writes to the wrong file. - Assuming
Activatealso selects a sheet. It does not. The previously active sheet in that workbook stays active. - Hard-coding names that include the path.
Workbooksexpects the name as shown in the title bar, not a full path. To open by path, useWorkbooks.Openinstead. - Relying on index numbers.
Workbooks(1)is not a fixed file; it changes with open order and with add-ins that create hidden workbooks. - Forgetting that
Activateneeds the workbook open. It does not open files — that isWorkbooks.Open.
Tips
- Turn on
Option Explicitat the top of every module so undeclared variables are caught at compile time rather than misbehaving at run time. - Use
Application.ScreenUpdating = Falsearound a batch of switches in long macros, then set it back toTrue, to cut flicker. - If you only need to read or write cells, skip activation entirely:
Workbooks("Book2.xlsx").Worksheets("Data").Range("A1").Value = 5works without switching windows and is faster and less disruptive. - Use the object browser (F2) to confirm which members a
Workbookobject actually exposes before guessing at method names. - Name workbooks you switch to often as constants at the top of the module so a rename means one edit, not twenty.
Troubleshooting
Run-time error 9, subscript out of range. The workbook name does not match an open workbook. Check spelling, the extension, and whether the file is actually open. Hidden workbooks still count, so a name you cannot see may still be valid.
Run-time error 1004. Usually a range or sheet reference made while the wrong workbook or sheet is active, or a protected workbook. Activate the workbook, then the sheet, then act on the range.
Nothing appears to happen. The macro may be switching between two workbooks that look identical, or the target is already active. Add a MsgBox ActiveWorkbook.Name after the call to confirm which file has focus.
The macro stops on a protected workbook. Activate works on protected workbooks, but selecting ranges on protected sheets does not. Unprotect with the correct password, or write values directly instead of selecting.
Macros are blocked. Check File > Options > Trust Center > Trust Center Settings > Macro Settings, and confirm the file sits in a trusted location. On Mac, check Excel > Preferences > Security. Do not lower security settings globally just to run one file.
Still stuck? Step through the code with F8 to see exactly which line fails, and confirm the workbook names in the Immediate window with ?Workbooks.Count and a loop printing each .Name.
Frequently Asked Questions
Can I use Select on a workbook in VBA?
No. The Workbook object has no Select method. Use Activate to bring a workbook to the front, and Select only on worksheets, ranges, charts and shapes.
What is the difference between Activate and Select for worksheets?
Activate makes one sheet the active sheet. Select can include several sheets at once, producing a grouped selection where typing affects every selected sheet — useful deliberately, dangerous by accident.
How do I switch to a workbook whose name I do not know?
Loop through the Workbooks collection and test each .Name, or capture the workbook in a variable when you open it with Set wb = Workbooks.Open(path) and reuse that variable.
Does Activate open a closed workbook?
No. It only works on workbooks already open in the same Excel instance. Use Workbooks.Open with a full path to open a file first.
Why does my macro write to the wrong workbook?
Almost always because it relies on ActiveWorkbook after something else changed focus. Qualify every reference with an explicit workbook object, or use ThisWorkbook when you mean the file holding the code.
Can I activate a workbook that is hidden?
Not directly. Set its Windows(1).Visible property to True first, then activate it. Hidden workbooks can still be read and written without activation.