Presentation is loading. Please wait.

Presentation is loading. Please wait.

Activate a range and manipulating activated range

Similar presentations


Presentation on theme: "Activate a range and manipulating activated range"— Presentation transcript:

1 ExCEL Macro (part 1I) 1. Activate a range and manipulating activated range 2.Offset 3. Int

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

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:

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.

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.

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.

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:

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:

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:

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:

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:

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 is middle gray? For some insights into Excel’s wonderful world of color, see the nearby sidebar “A quick and dirty color primer.”

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.

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:

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.

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:


Download ppt "Activate a range and manipulating activated range"

Similar presentations


Ads by Google