Sign up for our Excel webinar, times added weekly: https://www.excelcampus.com/blueprint-registration/
In this video I explain the different places to store macros, functions & VBA code in a workbook. We specifically look at the Code Module, Sheet Module, and ThisWorkbook Module to learn the differences between how each works.
Code Modules allow us to store regular macros (sub procedures) and functions (User Defined Functions, UDFs). This is the typical place we start writing and storing our macros. The macro recorder also creates code in a code module.
Each sheet in the workbook has a sheet object in the VB Editor. These are listed in the Microsoft Excel Objects folder for each open workbook in the Project Window. Double-clicking a sheet object opens its code module. Here we store regular macros & functions.
We can also Event Procedures that will run when the user takes an action in the worksheet. This could be actions like selecting a cell, activating a sheet, or deleting a sheet. There are a lot of events for the sheet object.
Each workbook contains one object called ThisWorkbook. The ThisWorkbook object is stored at the bottom of the Microsoft Excel Objects folder.
The main purpose of the ThisWorkbook module is to run event procedures (macros) when the user takes an action in the workbook. This could be when the user opens the workbook, before they close the workbook, when they select a worksheet, range, or even update a pivot table.
Checkout the full article and download the example file at: https://www.excelcampus.com/vba/code-modules-event-procedures/
Checkout my other Macros & VBA videos: https://www.youtube.com/playlist?list=PLKbOx3gUV_E87DSMLQFKdYy2XxwrcBA19
Register for my free training webinar on macros & VBA:
https://www.excelcampus.com/vba-webinar