Activate a sheet using VBA in Excel

Applying the Activate method on a Worksheet object makes it the active sheet, the sheet on top.

Select sheet

To be able to activate a sheet you first have to select it. Activating an object is simple, call the Activate method on the object. You will have to specify the object you want activated and that is where you have quite a few options which are discussed here.

Activate sheet by number

It is called like this:


ActiveWorkbook.Sheets(Index:=1).Activate

The above code activates the first sheet in the specified workbook.

The  Worksheet object here is obtained from the Workbook Sheets procedure .

Calling this method is equivalent to choosing the sheet's tab.

Activate sheet by name

Activate sheet with variable name

The most common way to refer to a sheet is by its name as displayed in the sheets tab:


ActiveWorkbook.Sheets(Index:="data2-17-14").Activate

If your macro will be used by others, you will want to add proper error handling, possibly like:


Dim ws As Worksheet
On Error Resume Next
Set ws = ActiveWorkbook.Sheets(Index:="data2-17-14")
On Error GoTo 0
If Not ws Is Nothing Then
    ws.Activate
Else
    
End If

Activate sheet by codename

You can also directly refer to a sheet using its CodeName property. This has the advantage that you can be sure the names sheet exists. , but this can only be done if the macro is in the same workbook as the sheet - which is often not the case.


Sheet1.Activate

Activate sheet based on cell value

An application for wanting to activate a sheet based on a cell value is where you have so many tabs that it becomes difficult to navigate. A possible solution would be to have a navigation worksheet listing the target sheets and having the sheet open that corresponds to the selected cell.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim strWorksheet As String: strWorksheet = Target.Value
	On Error Resume Next
    Worksheets(Index:=strWorksheet).Activate
End Sub

Select sheet and cell

A cell can only be selected if the sheet on which it lives is the active one. Therefore, generally you will precede the selection by activating its Parent. For example, instead of Application.Range("Sheet2!B2").Select you will be safer:


With Application.Range("Sheet2!B2")
    .Parent.Activate
    .Select
End With

Activate sheet in another workbook

To activate a sheet the workbook itself need not be the active workbook. You only have to identify the workbook, and the worksheet you want to activate and then do it:


Dim wb As Workbook
Set wb = Workbooks(Index:="data123.xlsx")
Dim ws As Worksheet
Set ws = wb.Sheets(Index:="Countries")
ws.Activate

Activate sheet without showing

You do not need to make a worksheet (or workbook!) active to be able to operate on it. The way to go is to declare a worksheet variable and instantiate it using a Set statement, as was done in the above section. Using variables this way instead of relying on the sheet being active is both faster and safer.


Author: Mark Uildriks
Reference: Worksheet.Activate() | Microsoft Learn
icon

Try the Code VBA editor add-in