To select a sheet in VBA, use the Worksheets or Sheets collection with the sheet’s name or index number, for example: Worksheets("Sheet1").Select. You can also use Sheets(1).Select to select the first sheet in the workbook. This article explains the different ways to select a sheet, when to use each, and common pitfalls to avoid.
Why Select a Sheet in VBA?
Selecting a sheet is often the first step in a macro that needs to read or write data on a specific worksheet. When you record a macro, Excel automatically generates code that selects sheets and ranges. While selecting is not always necessary (you can often work with a sheet without selecting it), it is useful when you want to change the user’s view, activate a sheet to run certain methods, or when you are building a macro step by step and want to see what happens.
Basic Ways to Select a Sheet
There are several ways to select a sheet in VBA. The most common methods are:
- By name:
Worksheets("Sheet1").Selectselects the sheet named “Sheet1”. - By index number:
Sheets(1).Selectselects the first sheet in the workbook (the order shown in the tab bar). - Using the ActiveSheet property:
ActiveSheet.Selectselects the currently active sheet (this is rarely needed). - Using the CodeName: If the sheet’s CodeName is known (e.g.,
Sheet1), you can useSheet1.Selectdirectly. The CodeName is the name shown in the VBA Project Explorer, not necessarily the tab name.
Example: To select a sheet named “Data” in the active workbook, use:
Worksheets("Data").Select
Selecting a Sheet by Index vs. Name
Using the index number is useful when you want to select the first, second, or last sheet without knowing its name. For example, Sheets(Sheets.Count).Select selects the last sheet. However, index numbers can change if sheets are moved or added, so using the name is generally safer. If you use a name that doesn’t exist, VBA will raise a runtime error. To avoid that, you can check if the sheet exists before selecting it.
How to Select a Sheet and a Range
Often you need to select a specific cell or range on a sheet. You can combine the sheet selection with a range selection in one line:
Worksheets("Sheet1").Range("A1").Select
This selects cell A1 on Sheet1. If you want to select a range without first selecting the sheet, you can use:
Worksheets("Sheet1").Range("A1:B10").Select
Note that when you select a range on a sheet that is not active, Excel will automatically activate that sheet first.
Common Errors and How to Avoid Them
One common error is trying to select a sheet that is not in the workbook. This gives a runtime error 9 (Subscript out of range). To avoid it, you can check if the sheet exists before selecting:
If WorksheetExists("Data") Then
Worksheets("Data").Select
Else
MsgBox "Sheet not found"
End If
Another issue is that Select will fail if the workbook is not active or if the sheet is hidden. Hidden sheets cannot be selected unless you unhide them first. Also, if you run a macro that selects a sheet while another workbook is active, you need to activate the correct workbook first.
Alternatives to Selecting a Sheet
In many cases, you don’t need to select a sheet at all. You can directly reference a sheet and its cells without changing the active sheet. For example, to write a value to cell A1 on Sheet1 without selecting it:
Worksheets("Sheet1").Range("A1").Value = 100
This is faster and doesn’t cause the screen to flicker. Using Select is often a byproduct of the macro recorder, but experienced VBA developers prefer to avoid it unless they need to change the user’s view or use a method that requires an active sheet (like Activate).
Selecting Multiple Sheets
You can select multiple sheets at once by using an array of names or indices. For example, to select Sheet1 and Sheet2:
Worksheets(Array("Sheet1", "Sheet2")).Select
This groups the sheets, similar to holding Ctrl and clicking tabs. When multiple sheets are selected, the active sheet is the first one in the array. To deselect all but one sheet, you can select just that sheet again.
Frequently Asked Questions
How do I select a sheet by its tab color? You cannot select a sheet directly by tab color. You need to loop through all sheets and check the Tab.Color property, then select the matching one.
What is the difference between Worksheets and Sheets? Worksheets refers only to worksheet objects, while Sheets includes all sheet types (worksheets, chart sheets, and dialog sheets). For most tasks, Worksheets is sufficient.
Why does my macro stop when I try to select a sheet? This usually happens if the sheet name is misspelled, the sheet is hidden, or the workbook is not active. Check the name and ensure the sheet is visible.
Can I select a sheet in another workbook? Yes, you must first reference the workbook, for example: Workbooks("Book1.xlsx").Worksheets("Sheet1").Select. However, you need to ensure that workbook is open.
Is there a way to select a sheet without activating it? No, selecting a sheet always activates it. If you want to work with a sheet without changing the active sheet, use direct references instead of Select.
Does selecting a sheet affect performance? Yes, excessive use of Select can slow down macros because it forces Excel to update the user interface. For better performance, avoid selecting unless necessary.