Excel does not open worksheets independently: a worksheet belongs to a workbook that is open in Excel. To check whether a named worksheet exists in a particular open workbook, test its Worksheets collection; to check whether the workbook itself is open, inspect Application.Workbooks. These are different checks, and the right one depends on what you mean by “open.”
What “open worksheet” means in VBA
In everyday language, someone may say a worksheet is open when they can see its tab, when its workbook is open, or when that sheet is currently displayed. VBA treats these as separate states. A workbook can be open while the requested worksheet is missing, hidden, or simply not the active sheet. A worksheet can also exist in a workbook that is currently closed, although you cannot refer to it through that workbook’s live Worksheets collection until the file is opened.
Start by identifying the question you need to answer:
- Is the workbook open in this Excel instance? Look through
Application.Workbooks. - Does the workbook contain a worksheet with this name? Look up the name in that workbook’s
Worksheetscollection. - Can a person see the sheet? Check the worksheet’s
Visibleproperty. - Is this the sheet currently displayed? Compare it with
ActiveSheet.
Using the precise test avoids a common bug: treating a sheet that is present but hidden as if it did not exist, or assuming the active workbook is necessarily the workbook your code intends to inspect.
Check whether a worksheet exists
For most macros, the useful check is whether a worksheet with a specified name exists in a known workbook. Put this function in a standard VBA module, such as one inserted through the Visual Basic Editor. It returns True when the named item is a worksheet and False when the lookup fails.
Public Function WorksheetExists(ByVal sheetName As String, ByVal wb As Workbook) As Boolean
Dim ws As Worksheet
' A failed name lookup means this workbook has no matching worksheet.
On Error Resume Next
Set ws = wb.Worksheets(sheetName)
On Error GoTo 0
WorksheetExists = Not ws Is Nothing
End Function
For example, with the macro stored in the workbook being checked, this call tests for a worksheet named Summary:
Sub CheckSummarySheet()
Dim found As Boolean
' ThisWorkbook means the workbook containing this VBA code.
found = WorksheetExists("Summary", ThisWorkbook)
If found Then
MsgBox "The Summary worksheet exists."
Else
MsgBox "The Summary worksheet was not found."
End If
End Sub
The function takes the workbook as an argument deliberately. That makes its target explicit and lets you reuse it for another workbook. The lookup is restricted to worksheets: a chart sheet named Summary does not count as a worksheet. Excel worksheet names are not case-sensitive for this lookup, so “Summary” and “summary” refer to the same sheet name. Leading or trailing spaces, however, are part of a name and can cause a lookup to fail.
Check whether the workbook is open
If the question is whether an Excel file is already open, check the workbooks in the current Excel application rather than looking for a worksheet. The following function compares workbook names without regard to letter case:
Public Function IsWorkbookOpen(ByVal targetName As String) As Boolean
Dim wb As Workbook
' Search the workbooks open in this Excel instance.
For Each wb In Application.Workbooks
If StrComp(wb.Name, targetName, vbTextCompare) = 0 Then
IsWorkbookOpen = True
Exit Function
End If
Next wb
End Function
Call it with a workbook name that includes its extension, such as Sales.xlsx. A matching name means the file is open in the Excel instance running this code; it does not mean every Excel process on the computer has been checked. If another Excel instance opened the file separately, that instance has its own Application.Workbooks collection.
Matching only wb.Name is convenient, but it is not a full-path comparison. If your workflow could involve files with identical names in different folders, compare each workbook’s FullName with the complete expected path instead. This helps prevent a macro from finding a different file that happens to be called Sales.xlsx.
Choose the correct workbook reference
The workbook argument matters as much as the sheet name. ThisWorkbook refers to the workbook that contains the running VBA code. ActiveWorkbook refers to the workbook currently in the foreground. They are often the same, but not always. For example, a macro stored in a personal macro workbook or an add-in can run while a separate workbook is active.
Use ThisWorkbook when the target is the file that contains the code. Use ActiveWorkbook only when the user’s currently selected workbook is intentionally the target. If your code has already opened or identified a workbook, keep a reference to that specific Workbook object and pass it to the function. This is clearer and safer than relying on whichever workbook happens to be active at a particular moment.
A typical call for a different workbook object, named sourceWb in your procedure, is WorksheetExists("Data", sourceWb). The function checks the open workbook object you supply; it does not search every workbook automatically. If you need to search all open workbooks, loop through Application.Workbooks and call the worksheet test for each one.
Check whether a worksheet is visible
A successful existence check does not guarantee that the worksheet tab is visible. Excel has three worksheet visibility states: xlSheetVisible, xlSheetHidden, and xlSheetVeryHidden. A normal hidden sheet can be restored through Excel’s Unhide command. A very hidden sheet is not offered in that dialog and must be made visible through VBA or the Visual Basic Editor’s properties.
After confirming that the worksheet exists, inspect its state like this:
Sub ReportSheetVisibility()
Dim ws As Worksheet
' Check existence first so the name lookup does not stop the procedure.
If WorksheetExists("Summary", ThisWorkbook) Then
Set ws = ThisWorkbook.Worksheets("Summary")
If ws.Visible = xlSheetVisible Then
MsgBox "Summary is visible."
Else
MsgBox "Summary exists but is hidden."
End If
End If
End Sub
This reports visibility, not whether the sheet is active. Hidden sheets are still members of the workbook’s Worksheets collection, so the existence function returns True for them. That distinction is useful when a macro should write to a hidden data sheet without displaying it.
Check whether the worksheet is active
“Active” means currently selected or displayed; it is not a synonym for “exists” or “visible.” A worksheet can exist and be visible while another sheet is active. To test whether a particular worksheet in the code’s workbook is the active sheet, first confirm the active workbook and then compare the active sheet’s name:
Sub CheckActiveSummary()
Dim isActive As Boolean
' Confirm the correct workbook before checking its selected sheet.
If ActiveWorkbook Is ThisWorkbook Then
isActive = (ActiveSheet.Name = "Summary")
End If
If isActive Then
MsgBox "Summary is the active sheet."
Else
MsgBox "Summary is not the active sheet in this workbook."
End If
End Sub
The workbook comparison prevents a sheet with the same name in a different open workbook from being mistaken for the target. This code is intended for a workbook that is open in the Excel interface. In automated or background procedures, it is often better to work directly with a worksheet object than to activate or select sheets. Most VBA operations, such as reading a range or writing a value, do not require the target worksheet to be active.
Common problems and edge cases
- Checking the wrong workbook:
ActiveWorkbookcan change as the user switches windows, and may differ fromThisWorkbook. Pass a deliberate workbook reference. - Confusing a hidden sheet with a missing sheet: Hidden worksheets remain in the collection. Check
Visibleseparately if visibility matters. - Looking in the wrong collection:
Worksheetscontains worksheet tabs;Sheetscan also include chart sheets. Choose based on the kind of sheet you need. - Using a slightly different name: Extra spaces or a misspelled name can make the lookup fail. If names come from cells or user input, inspect the actual text before changing the lookup.
- Assuming the workbook search covers all Excel processes:
Application.Workbooksonly represents the current Excel application instance.
The sample existence function uses On Error Resume Next only around the single operation that can fail when a name is absent, then immediately restores normal error handling. Avoid leaving that setting active across a large procedure: it can conceal unrelated errors and make debugging difficult.
Where to put and run the VBA
In desktop Excel for Microsoft 365 on Windows, open the Visual Basic Editor from the Developer tab using Developer > Visual Basic. If the Developer tab is missing, enable it through File > Options > Customize Ribbon. In Excel for Mac, use Developer > Visual Basic; the ribbon settings used to show Developer can vary somewhat between Mac releases. The VBA examples work in desktop Excel for Windows and Mac, although code that relies on Windows-specific APIs or file paths may need changes on Mac.
Excel for the web can open and edit many macro-enabled workbooks, but it cannot run VBA macros. Use desktop Excel to execute these procedures. Save the file in a macro-enabled format such as .xlsm if you need to retain VBA code. Before running any macro, save a copy of the workbook: macro actions cannot be undone with Ctrl+Z. The examples here only inspect workbook state and display messages, but that precaution matters when adapting them into code that changes data.
Frequently Asked Questions
Can VBA check whether a worksheet tab is open?
Not as a separate open-file state, because worksheets are parts of workbooks. Check whether the workbook is open, whether the worksheet exists in it, and whether the sheet is visible or active, depending on what “open” means in your case.
Does a hidden worksheet count as existing?
Yes. A hidden or very hidden worksheet is still in the workbook’s Worksheets collection. The existence check returns True; use the Visible property to determine whether Excel displays its tab.
Should I use ThisWorkbook or ActiveWorkbook?
Use ThisWorkbook when you mean the workbook containing the VBA code. Use ActiveWorkbook when the workbook currently in front of the user is intentionally the target. For a specific already identified file, pass its workbook object directly.
Does the check include chart sheets?
No. The example checks Worksheets, which contains worksheet objects. A chart sheet is a different sheet type. If you need to look for either a worksheet or a chart sheet by name, consider the workbook’s Sheets collection and handle the returned object’s type appropriately.
Can VBA detect a workbook open in another Excel window?
If the window belongs to the same Excel application instance, the workbook is generally represented in that instance’s Application.Workbooks collection. A separate Excel process has a separate application and collection, so this simple check does not search it.
Related
- Microsoft Excel Protect Sheet: Steps, Limits, and Tips
- Microsoft Excel Named Ranges: How They Work and Help
- Microsoft Excel CSV Import: Steps, Settings, and Fixes
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Copy Paste Special: Values, Formats, and MoreIn Excel VBA, copy a range and call PasteSpecial on the destination, specifying what to paste. For example,…
- Creating Macros in Google Sheets: A Practical GuideTo create a macro in Google Sheets, open the spreadsheet in a web browser and choose Extensions >…
- Excel VBA: Find the First Non-Empty Row in a ColumnTo find the first non-empty row in an Excel column with VBA, use the range’s Find method with…
- Microsoft Excel VBA For Loops: Syntax and ExamplesIn Microsoft Excel VBA, a For...Next loop repeats instructions a set number of times, while For Each...Next repeats…
- VBA Loop Code: Examples for Excel MacrosVBA loop code repeats an instruction for a set number of times, for each item in a collection,…
- VBA Copy and Paste a Range of Cells in ExcelTo copy a range of cells with VBA, use sourceRange.Copy Destination:=destinationRange. For example, Worksheets("Sheet1").Range("A1:C10").Copy Destination:=Worksheets("Sheet2").Range("A1") copies the range…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.