Get Excel Objects
In Excel, you always get one object from another, and everything starts with the Application object. So, if you want to change the font of cell C2 to bold, you would simply type:
Application.ActiveWorkbook.ActiveSheet.Range("C2").Font.Bold = TrueNot really! Application, ActiveWorkbook, and ActiveSheet are all global members in Excel, so you can shorten your code to:
ActiveWorkbook.ActiveSheet.Range("C2").Font.Bold = Trueor:
ActiveSheet.Range("C2").Font.Bold = Trueor more likely:
Range("C2").Font.Bold = TrueEach of the members in the original line of code returns an object reference that navigates from the top-level object (the Excel Application object) to the low-level object (a Font object) for which you want to set the Bold property. The order of objects looks like this:
Application → Workbook → Worksheet → Range → Font (set Bold property) |
In other words, Excel’s objects are arranged hierarchically, but global members provide shortcuts through that hierarchy. Table 4-1 lists some commonly used shortcuts for navigating to Excel objects.
Table 4-1. Excel’s global shortcut members
|
Member |
Returns |
Use to |
|---|---|---|
|
|
|
Work with the currently selected cell or get the upper-lefthand corner of a selected block of cells. |
|
|
|
Get the chart that currently has focus. |
|
|
|
Get the sheet that has focus. The returned object may be a |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access