Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Worksheet.Parent: What It Returns and How to Use It

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

In Excel VBA, Worksheet.Parent returns the workbook that contains the worksheet. It is the shortest, most reliable way to get from a sheet object back to its Workbook object, so you can read the workbook name, loop through its sheets, save it, or hand it to another procedure. If you have a reference to a sheet — say ws — then ws.Parent is that sheet's workbook, and ws.Parent.Name gives you the file name. Nothing needs to be installed; you only need the Excel desktop app and the VBA editor.

What You'll Need

  • Excel for Windows or Excel for Mac (desktop). VBA is not available in Excel for the web.
  • A workbook saved as .xlsm (macro-enabled) if you want your code to persist. Unsaved .xlsx files can run code but will not keep it.
  • The Developer tab enabled, or press Alt+F11 (Windows) or Option+F11 (Mac) to open the VBA editor directly.
  • Macro security set to allow macros in your own file. In the Trust Center, the setting is under Macro Settings. Labels can differ slightly by version and region.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 on Windows or Option+F11 on Mac. The editor opens in its own window.
  2. Insert a module. In the editor menu choose Insert > Module. A blank code pane appears. Code in a standard module is the easiest place to test Parent.
  3. Write a minimal procedure. Type the following, replacing the sheet name with one that exists in your workbook:
    Sub ShowParent()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        MsgBox ws.Parent.Name
    End Sub
    This displays the name of the workbook that owns Sheet1.
  4. Run it. Place the cursor inside the procedure and press F5, or use Run > Run Sub/UserForm. A message box should show your file name. If you get a "Subscript out of range" error, the sheet name does not exist — check the tab name exactly, including spaces.
  5. Use Parent to reach workbook-level properties. Once you have ws.Parent, you can chain properties. For example, ws.Parent.FullName returns the full path and file name, and ws.Parent.Sheets.Count returns the number of sheets in that workbook. This is useful when your code receives a sheet from another procedure and you do not want to hard-code a workbook name.
  6. Loop through sheets using Parent. A common pattern is to iterate every sheet in the workbook that owns a given sheet:
    Sub ListSheets()
        Dim ws As Worksheet, s As Worksheet
        Set ws = ActiveSheet
        For Each s In ws.Parent.Worksheets
            Debug.Print s.Name
        Next s
    End Sub
    Press Ctrl+G to open the Immediate Window and see the output.
  7. Save the workbook. Save the file as .xlsm so the macro is retained. On Mac, use File > Save As and choose Excel Macro-Enabled Workbook.

Common Mistakes

Confusing Parent with ThisWorkbook. ThisWorkbook always refers to the workbook containing the running code. ws.Parent refers to the workbook that owns the specific sheet object. They are the same only when the sheet lives in the code's own workbook. If your code opens another workbook and works with its sheets, Parent correctly follows the sheet, while ThisWorkbook does not.

Using Parent on a Range. Range("A1").Parent returns a Worksheet, not a Workbook. To reach the workbook from a range you need Range("A1").Parent.Parent. Each Parent steps up one level in the object model: Range → Worksheet → Workbook → Application.

Assuming Parent is a Worksheet. If you declare a variable as Worksheet and assign someRange.Parent, it works, but assigning someSheet.Parent to a Worksheet variable fails at runtime because that returns a Workbook. Match the declared type to the level you are stepping to.

Relying on ActiveSheet instead of an explicit reference. ActiveSheet.Parent changes whenever the user clicks another workbook. Pass the sheet object into your procedure instead of reading the active one.

Tips

Use Parent in reusable procedures that accept a Worksheet argument. The procedure can then read ws.Parent.Name without knowing which file it will be called from. This keeps code portable between workbooks.

Combine Parent with TypeName while debugging: Debug.Print TypeName(ws.Parent) should print Workbook. If it prints something else, you are at a different level of the object model than you think.

For chart sheets, Parent also returns the workbook, so the same pattern works whether the sheet is a worksheet or a chart sheet. Declare the variable as Object if you need to handle both.

Remember that Parent is read-only. You cannot assign a new workbook to it; use Worksheets.Add or Workbook.Move to change where a sheet lives.

Troubleshooting

"Object variable or With block variable not set." You used ws.Parent before assigning ws. Add Set ws = ... before the line that uses Parent.

"Subscript out of range." The sheet name passed to Worksheets() does not exist in ThisWorkbook. Check the tab name; trailing spaces are a frequent cause.

Wrong workbook name appears. You are probably reading ActiveSheet.Parent while a different workbook is active. Replace it with an explicit sheet reference.

Macro will not run. Check the Trust Center macro settings and confirm the file is saved as .xlsm. If the file came from another computer, Windows may block it; open the file's properties and look for the unblock option before opening it in Excel.

If that did not work, close and reopen Excel, then re-open the file and try again. If the editor itself will not open, check whether the workbook is protected or the VBA project is password-protected; you will need the password from whoever set it.

Frequently Asked Questions

What does Worksheet.Parent return?

It returns the Workbook object that contains the worksheet.

Is Parent the same as ThisWorkbook?

No. ThisWorkbook is the workbook running the code; Parent is the workbook that owns the specific sheet object. They match only when the sheet is in the code's own workbook.

How do I get the workbook name from a sheet?

Use ws.Parent.Name for the file name, or ws.Parent.FullName for the full path and name.

Can I use Parent on a Range or Chart?

Yes, but each returns a different level. Range.Parent returns a Worksheet; Worksheet.Parent returns a Workbook. Chain .Parent again to step up further.

Does Parent work in Excel for the web?

No. VBA is not available in Excel for the web; you need the desktop app.

Can I change a sheet's Parent?

No. Parent is read-only. To move a sheet to another workbook, use the sheet's Copy or Move method with a destination workbook.