VBA Tutorial

Workbooks & Worksheets

Managing files and sheets in the Excel Object Model

The Hierarchy

VBA follows a clear object hierarchy. To be safe and precise, you should always reference your objects from the top down.
  1. Application: Excel itself.
  2. Workbook: The file (.xlsx or .xlsm).
  3. Worksheet: The specific tab inside the file.
  4. Range: The cells.
vba
ThisWorkbook.Worksheets("Data").Range("A1")

Managing Workbooks

ActionCode
OpenWorkbooks.Open("C:\file.xlsx")
CloseActiveWorkbook.Close SaveChanges:=True
AddWorkbooks.Add
SaveActiveWorkbook.SaveAs "NewFile.xlsx"

Managing Worksheets

Wait! You can easily loop through all the sheets in a workbook to perform a task (like hiding them or clearing them).
vba
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Main" Then
        ws.Visible = xlSheetHidden
    End If
Next ws

ThisWorkbook vs. ActiveWorkbook

  • ThisWorkbook: The file where the VBA code is currently running (Always safe!).
  • ActiveWorkbook: The file currently on top of the user's screen (Unpredictable!).
[!IMPORTANT] Always prefer ThisWorkbook unless you specifically intend to modify a different file.

Summary

TermDescription
WorkbooksThe collection of all open files
WorksheetsThe collection of all tabs in a file
AddCreate a new workbook or sheet
DeleteRemove a workbook or sheet
NameThe label on the worksheet tab
Best ForMulti-file reporting and consolidating data
Logic"Navigating the Office ecosystem"
SafetyTurn off Application.DisplayAlerts before deleting sheets
ModernCombine with Dir() to process a whole folder of files
StandardUse meaningful names for your sheets, not "Sheet1"
IdentityThe fundamental structure of an Excel project