The Hierarchy
VBA follows a clear object hierarchy. To be safe and precise, you should always reference your objects from the top down.
- Application: Excel itself.
- Workbook: The file (
.xlsxor.xlsm). - Worksheet: The specific tab inside the file.
- Range: The cells.
vba
Managing Workbooks
| Action | Code |
|---|---|
| Open | Workbooks.Open("C:\file.xlsx") |
| Close | ActiveWorkbook.Close SaveChanges:=True |
| Add | Workbooks.Add |
| Save | ActiveWorkbook.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
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 preferThisWorkbookunless you specifically intend to modify a different file.
Summary
| Term | Description |
|---|---|
| Workbooks | The collection of all open files |
| Worksheets | The collection of all tabs in a file |
| Add | Create a new workbook or sheet |
| Delete | Remove a workbook or sheet |
| Name | The label on the worksheet tab |
| Best For | Multi-file reporting and consolidating data |
| Logic | "Navigating the Office ecosystem" |
| Safety | Turn off Application.DisplayAlerts before deleting sheets |
| Modern | Combine with Dir() to process a whole folder of files |
| Standard | Use meaningful names for your sheets, not "Sheet1" |
| Identity | The fundamental structure of an Excel project |