Understanding Object Properties and Hierarchy in Excel VBA

Slide Note
Embed
Share

Exploring how to activate, manipulate, and work with different Excel object collections such as Workbooks, Worksheets, and Charts in VBA. Learn to navigate object hierarchies, access specific objects, and manage object properties to enhance your Excel macro development skills.


Uploaded on Oct 11, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. EXCEL MACRO (PART 1I) 1. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE 2.OFFSET 3. INT 1

  2. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE Here are a few examples of commonly used collections: Workbooks: A collection of all currently open Workbook objects Worksheets: A collection of all Worksheet objects contained in a particular Workbook object Charts: A collection of all Chart objects (chart sheets) contained in a particular Workbook object Sheets: A collection of all sheets 2

  3. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE You can work with an entire collection of objects in one fell swoop. More often, however, you need to work with a specific object in a collection (such as a particular worksheet in a workbook). To reference a single object from a collection, you put the object s name or index number in parentheses after the name of the collection, like this: If Sheet1 is the first (or only) worksheet in the collection, you can also use the following reference: 3

  4. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE Navigating through the hierarchy If you want to work with the Application object, it s easy: You start by typing Application. To navigate farther to a specific worksheet, add a dot operator and access the Worksheets collection object. Not far enough yet? If you really want to get the value from cell A1 on the first Worksheet of the Workbook named Book1.xlsx, you need to navigate one more level to the Range object. 4

  5. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE Every object has properties. You can think of properties as attributes that describe the object. An object s properties determine how it looks, how it behaves, and even whether it is visible. Using VBA, you can do two things with an object s properties: Examine the current setting for a property. Change the property s setting. 5

  6. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE For example, a single-cell Range object has a property called Value. The Value property stores the value contained in the cell. You can write VBA code to display the Value property, or you may write VBA code to set the Value property to a specific value. The following macro uses the VBA builtin MsgBox function to bring up a box that displays the value in cell A1 on Sheet1 of the active workbook. 6

  7. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE In addition to properties, objects have methods. A method is an action you perform with an object. A method can change an object s properties or make the object do something. This simple example uses the ClearContents method on a Range object to erase the contents of 12 cells on the active sheet: 7

  8. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE The following example activates Sheet1 (in the active workbook) and then copies the contents of cell A1 to cell B1 by using the Range object s Copy method. In this example, the Copy method has one argument the destination range for the copy operation: 8

  9. ACTIVATE A RANGE AND MANIPULATING ACTIVATED RANGE Another way to specify an argument for a method is to use the official name of the argument followed by a colon and an equal sign. Using named arguments is optional, but doing so can often make your code easier to understand. The second statement in the CopyOne procedure could be written like this: 9

  10. THE OFFSET PROPERTY The Offset property provides another handy means for referring to ranges. This property, which operates on a Range object and returns another Range object, lets you refer to a cell that is a particular number of rows and columns away from another cell. Like the Cells property, the Offset property takes two arguments. The first argument represents the number of rows to offset; the second represents the number of columns to offset. The following expression refers to a cell one row below cell A1 and two columns to the right of cell A1. In other words, this refers to the cell commonly known as C2: 10

  11. THE OFFSET PROPERTY The Offset property can also use negative arguments. A negative row offset refers to a row above the range. A negative column offset refers to a column to the left of the range. The following example refers to cell A1: And, as you may expect, you can use 0 as one or both of the arguments for Offset. The following expression refers to cell A1: Here s a statement that inserts the time of day into the cell to the right of the active cell: 11

  12. THE INTERIOR PROPERTY Here s yet another example of a property that returns an object. A Range object s Interior property returns an Interior object (strange name, but that s what it s called). For example, the following statement changes the Color property of the Interior object contained in the Range object: In other words, this statement changes the cell s background to middle gray. What s that? You didn t know that 8421504 is middle gray? For some insights into Excel s wonderful world of color, see the nearby sidebar A quick and dirty color primer. 12

  13. THE INTERIOR PROPERTY You can perform copy and paste operations in VBA by using the Copy and Paste methods. Note that two different objects come into play. The Copy method is applicable to the Range object, but the Paste method applies to the Worksheet object. It actually makes sense: You copy a range and paste it to a worksheet. 13

  14. THE INTERIOR PROPERTY Notice that in the preceding example, the ActiveSheet object is used with the Paste method. This is a special version of the Worksheet object that refers to the currently active worksheet. Also notice that the macro selects the range before copying it. However, you don t have to select a range before doing something with it. In fact, the following procedure accomplishes the same task as the preceding example by using a single statement: 14

  15. THE INTERIOR PROPERTY The Clear method The Clear method deletes the contents of a range, plus all the cell formatting. For example, if you want to zap everything in column D, the following statement does the trick: You should be aware of two related methods. The ClearContents method deletes the contents of the range but leaves the formatting intact. The ClearFormats method deletes the formatting in the range but not the cell contents. 15

  16. THE INTERIOR PROPERTY The Delete method Clearing a range differs from deleting a range. When you delete a range, Excel shifts the remaining cells around to fill up the range you deleted. The following example uses the Delete method to delete row 6: The following statement deletes a range and then fills the resulting gap by shifting the other cells to the left: 16

Related


More Related Content