Name

workbook.ActiveSheet

Synopsis

Returns a reference to the worksheet, chart sheet, Excel 4.0 macro sheet, or Excel 5.0 dialog sheet that currently has focus. In most cases, ActiveSheet returns a reference to the active worksheet, but it is important to make sure that it is a worksheet before proceeding in code. For example, the following code checks whether ActiveSheet is a worksheet before performing some task:

Dim ws As Worksheet
If TypeName(ActiveSheet) = "Worksheet" Then
    Set ws = ActiveSheet
    ws.Cells(1, 1) = 42
    ' Some other code...
Else
    MsgBox "Please activate a worksheet."
End If

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.