I’ve no idea how many of you know this little gem, but it’s something I use a great deal. If it’s common knowledge, forgive me for wasting your time!
When we’re in the VBE’s Immediate Window and code is not running, the term
<em>ThisWorkbook</em> refers to the workbook that contains the VBA project currently selected in the Project Explorer. This is particularly useful when dealing with addins or hidden workbooks, as we can easily check or change their properties, or close them, directly in the Immediate window. For example, we can see exactly where an addin is loaded from by selecting it’s project in the Project Explorer and typing the following in the Immediate Window:
And we can close it using:
Just be careful about which project is selected at the time, as clicking a code pane window will select that project in the Project Explorer! Obviously, when VBA code is running and we’re in debug mode, ThisWorkbook refers to the workbook containing the executing code, regardless of which one is selected in the Project Explorer