In Excel VBA, the current worksheet is usually the sheet the user is looking at, and VBA refers to it as ActiveSheet. Use it when an action should follow the user’s current selection; use an explicit worksheet reference, such as ThisWorkbook.Worksheets("Summary"), when the code must always work on a particular sheet.
What “current worksheet” means in VBA
Excel can have several workbooks open, and each workbook can contain multiple worksheets. At any moment, one sheet is active in the Excel window. That active sheet is what people commonly mean by the “current worksheet.” In VBA, the application-level property ActiveSheet identifies it.
The phrase can be ambiguous, however. It might mean the sheet currently visible to the user, the sheet containing the code, or a specific sheet the macro is meant to edit. Those are not always the same. A macro launched from a button, the Macro dialog, or another workbook can run while a different workbook or sheet is active than the writer expected.
It is useful to separate these references:
ActiveSheetmeans the currently active sheet in Excel.ThisWorkbookmeans the workbook that contains the running VBA code.ActiveWorkbookmeans the workbook currently active in Excel’s window.ActiveCellmeans the selected cell on the active sheet, if the active sheet is a worksheet.
These names describe different things. Choosing the right one is more important than memorizing a single “current sheet” command.
How ActiveSheet works
ActiveSheet is an Excel application property. It returns the active sheet object, not necessarily a worksheet: a workbook can also have a chart sheet, for example. This is why code that expects worksheet features should either check the object type or use a known worksheet reference.
When the active sheet is a worksheet, you can use its properties and methods to read or change cells. For example, ActiveSheet.Name gives its tab name, and ActiveSheet.Range("A1") refers to cell A1 on it. Qualifying the range with the sheet is important: an unqualified Range("A1") in a standard module can depend on the active sheet and make code harder to reason about.
The active sheet can change while a macro runs. A call to Activate changes which sheet is active, and a user can also switch sheets before starting a macro. A procedure that repeatedly consults ActiveSheet may therefore act on a different sheet later than it did at the start. If the intended target should stay fixed, store a worksheet reference in a variable and use that variable throughout.
When to use ActiveSheet and when to use ThisWorkbook
Use ActiveSheet when the macro is deliberately meant to operate on whichever worksheet the user has selected. Typical examples include formatting the currently open report or displaying the name of the sheet in view. It is convenient for interactive, user-directed tasks, but it makes the result depend on Excel’s current state.
Use ThisWorkbook.Worksheets("Summary") when the macro must always update the Summary worksheet in the workbook that stores the code. This is often safer for repeatable processes and automated reports. The worksheet name must match an existing tab name; otherwise, VBA raises an error. If the target is in another workbook, qualify it with the appropriate workbook object rather than assuming the active workbook is the right one.
A common misconception is that ThisWorkbook means “the workbook currently on screen.” It does not. If code is stored in a separate add-in or in the Personal Macro Workbook, ThisWorkbook refers to that file. In that situation, ActiveWorkbook may be the workbook the user wants to edit, but it is still worth checking that it is the intended file before making changes.
Another distinction: Worksheets contains worksheet tabs, while Sheets can include chart sheets as well. If the procedure requires cells and ranges, use a worksheet reference rather than assuming every sheet is a worksheet.
Examples for finding and editing the current sheet
The following macro reports the name of the active worksheet. It checks the sheet type first, so it can give a useful message rather than treating a chart sheet as a worksheet.
Sub ShowActiveWorksheetName()
Dim ws As Worksheet
' Check that the active sheet is a worksheet.
If TypeOf ActiveSheet Is Worksheet Then
Set ws = ActiveSheet
MsgBox "Current worksheet: " & ws.Name
Else
MsgBox "The active sheet is not a worksheet."
End If
End Sub
To edit a cell on the current worksheet, assign the active worksheet to a variable and qualify the cell reference. With this example, the text goes into A1 of whichever worksheet is active when the macro runs.
Sub WriteToActiveWorksheet()
Dim ws As Worksheet
' Stop if the active sheet is a chart sheet or another sheet type.
If Not TypeOf ActiveSheet Is Worksheet Then Exit Sub
Set ws = ActiveSheet
' Write to A1 on the captured worksheet.
ws.Range("A1").Value = "Reviewed"
End Sub
If the task belongs on a fixed tab, qualify the reference instead. This example changes A1 on the Summary worksheet in the workbook containing the macro, regardless of which sheet is active. The tab named Summary must exist in that workbook.
Sub UpdateSummarySheet()
Dim ws As Worksheet
' Use a specific worksheet in the workbook that stores this code.
Set ws = ThisWorkbook.Worksheets("Summary")
ws.Range("A1").Value = "Updated"
End Sub
Before running any macro that changes workbook content, save a copy of the workbook. VBA actions generally cannot be reversed with Ctrl+Z, so a backup is the simplest way to recover from an unintended edit.
How to run a macro on the sheet you intend
In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, place a procedure in a standard module, and run it from the editor or from Developer > Macros. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon and select Developer. The workbook must be saved in a macro-enabled format such as .xlsm if it needs to retain VBA code.
Before running a macro that uses ActiveSheet, click the intended worksheet and confirm the correct workbook is active. If you launch the code from a button or another workbook, check where that button’s code is stored and which workbook is active. A macro can be correct in syntax yet produce the wrong result because it is acting on a different active sheet.
For a fixed target, it is usually better not to activate the sheet at all. Refer to it directly and edit its range. Activating sheets just to reach their cells creates extra dependence on the interface and can cause the visible selection to jump around. When you do need to make a sheet visible or bring it to the foreground, use the worksheet’s Activate method deliberately.
Excel for Mac also supports VBA, although the location of some controls and keyboard shortcuts differs from Windows. The object model distinctions—ActiveSheet, ThisWorkbook, and ActiveWorkbook—work the same way. Excel for the web can open and edit many macro-enabled workbooks, but it cannot create, run, or edit VBA macros; use the desktop Excel application for those tasks.
Common mistakes and how to avoid them
- Assuming the active sheet is always a worksheet: A chart sheet can be active. Test the type before using worksheet-only members such as
Range. - Confusing ActiveWorkbook with ThisWorkbook: The first is the workbook active in Excel; the second holds the code. This difference matters especially for macros stored in PERSONAL.XLSB or an add-in.
- Using an unqualified Range or Cells reference: In standard-module code, qualify it with a worksheet variable, such as
ws.Range("A1"), to make the target explicit. - Changing the active sheet mid-macro: If the code activates another sheet and then uses
ActiveSheetagain, the reference now points to the newly active sheet. Keep a worksheet variable when the original target must remain fixed. - Using the wrong collection:
Sheets("Summary")can refer to a chart sheet or worksheet. UseWorksheets("Summary")when the code requires worksheet cells. - Expecting VBA to run in a browser: Excel for the web does not execute VBA. Open the workbook in desktop Excel to run its macros.
These problems are often described as “VBA picked the wrong worksheet,” but the underlying issue is usually an implicit reference. Naming the workbook and worksheet in code makes the target visible to anyone reading or maintaining the macro.
Safe use and workbook considerations
Macros can modify or delete data, and Excel does not provide a normal undo history for VBA changes. Keep a backup before running unfamiliar code, and inspect code before allowing it to run. Only enable macros from sources you trust; a macro-enabled file can contain code that performs actions beyond ordinary cell editing.
For a procedure that only reads data, the risk of accidental damage is lower, but the destination still matters. For a procedure that writes values, clears ranges, deletes sheets, or saves files, verify the workbook and target sheet explicitly. If a macro is intended for reuse by other people, avoid relying on whichever tab happens to be selected and provide clear instructions about its target.
Macros are stored in formats that support VBA, such as .xlsm and Excel add-in files. Saving a VBA workbook as a macro-free .xlsx file removes the VBA project. Choose the format that matches how the workbook will be used, and keep a separate copy if you need both a macro-enabled original and a macro-free distribution file.
Frequently Asked Questions
How do I get the current worksheet name in VBA?
If the active sheet is a worksheet, use ActiveSheet.Name. For robust code, first check that the active sheet is a worksheet, then assign it to a Worksheet variable and read that variable’s Name property.
Is ActiveSheet the same as the selected worksheet?
In ordinary use, the active worksheet is the sheet currently shown and selected in Excel. But “selected” can also refer to selected cells or multiple selected sheet tabs. ActiveSheet identifies the active sheet object; it does not describe the cell selection.
What is the difference between ActiveSheet and ThisWorkbook?ActiveSheet identifies the current sheet in Excel, while ThisWorkbook identifies the workbook containing the VBA code. They answer different questions and can refer to objects in different workbooks.
How do I refer to a specific worksheet instead of the current one?
Use a fully qualified reference such as ThisWorkbook.Worksheets("Summary"). This points to the Summary worksheet in the workbook that contains the code, so confirm that the sheet name and workbook are correct.
Can ActiveSheet refer to a chart?
Yes. A chart sheet is a kind of sheet and can be active, but it is not a worksheet and does not provide worksheet cell ranges. Check the object type or use a specific worksheet reference before accessing cells.
Can I run Excel VBA in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in the desktop version of Excel for Windows or Mac to use its VBA code.
Related
- Excel Page Break Dotted Lines: How to Move Them in Print
- Excel Merge Cells and Keep All Text Without Data Loss
- Excel Merged Cells: Do They Need to Be the Same Size?
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: Find the Minimum Value in a Range or ArrayTo find a range’s minimum in Excel VBA, use Application.WorksheetFunction.Min, for example, Application.WorksheetFunction.Min(Range("B2:B20")). For a macro you can…
- Excel VBA Send Email in New Outlook: What Actually WorksExcel VBA cannot directly automate the new Outlook for Windows: the new app does not provide the classic…
- VBA Macros in Google Sheets: What Works InsteadNo. Google Sheets cannot run VBA macros: VBA is Microsoft Excel’s programming language, and Sheets uses Google Apps…
- Microsoft Excel VBA: What It Is and How to Use ItMicrosoft Excel VBA is the programming language built into the desktop versions of Excel that lets you automate…
- Excel VBA Filter Range: AutoFilter Examples and FixesTo filter a range with Excel VBA, use the range’s AutoFilter method and specify the field number and…
- Excel VBA: Find the First Empty Cell in a ColumnTo find the first empty cell in an Excel column with VBA, start at the first row you…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.