Excel VBA Programming For Dummies. Dick Kusleika
Чтение книги онлайн.

Читать онлайн книгу Excel VBA Programming For Dummies - Dick Kusleika страница 24

Название: Excel VBA Programming For Dummies

Автор: Dick Kusleika

Издательство: John Wiley & Sons Limited

Жанр: Программы

Серия:

isbn: 9781119843092

isbn:

СКАЧАТЬ how it looks, how it behaves, and even whether it’s 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.

       Sub ShowValue() Contents = Worksheets("Sheet1").Range("A1").Value MsgBox ContentsEnd Sub

Snapshot of the message box displays a Range object’s Value property.

      FIGURE 4-1: This message box displays a Range object’s Value property.

      

MsgBox is a very useful function. You can use the MsgBox function, for example, to display results while Excel executes your VBA code. You find out more about this function in Chapter 15, so be patient (or flip ahead and read all about it).

      The code in the preceding example displays the current setting of a cell’s Value property. What if you want to change the setting for that property? The following macro changes the value in cell A1 by changing the cell’s Value property:

       Sub ChangeValue() Worksheets("Sheet1").Range("A1").Value = 994.92End Sub

      After Excel executes this procedure, cell A1 on Sheet1 of the active workbook contains the value 994.92. If the active workbook doesn’t have a sheet named Sheet1, the result of executing that macro is an error message. VBA just follows instructions, and it can’t work with a sheet that doesn’t exist.

      Each object has its own set of properties, although some properties are common to many objects. For example, many (but not all) objects have a Visible property. Most objects also have a Name property.

      Some object properties are read-only properties, which means that your code can get the property’s value, but it can’t change it. For example, a Workbook object has a Name property that returns the workbook’s name. You can’t change the Name property directly because it’s read-only. To change a workbook’s name, you must use the Save or SaveAs methods.

A collection is also an object. This means that a collection also has properties. For example, you can determine how many workbooks are open by accessing the Count property of the Workbooks collection. The following VBA procedure displays a message box that tells you how many workbooks are open:

       Sub CountBooks() MsgBox Workbooks.CountEnd Sub

      For more about collections, see the previous section, “Referring to objects.”

      Taking action with object methods

      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:

       Sub ClearRange() Range("A1:A12").ClearContentsEnd Sub

      Some methods take one or more arguments. An argument is a value that further specifies the action to perform. You place the arguments for a method after the method, separated by a space. Multiple arguments are separated by a comma.

      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, which is the destination range for the copy operation:

       Sub CopyOne() Worksheets("Sheet1").Activate Range("A1").Copy Range("B1")End Sub

      Notice that the worksheet reference is omitted when referring to the Range objects. You can do this safely due to the statement to activate Sheet1 (using the Activate method).

       Range("A1").Copy Destination:=Range("B1")

Snapshot of the VBE displays a list of arguments while you type.

      FIGURE 4-2: The VBE displays a list of arguments while you type.

      Because a collection is also an object, collections have methods. The following macro uses the Add method for the Workbooks collection:

       Sub AddAWorkbook() Workbooks.AddEnd Sub

      As you might expect, this statement creates a new workbook. In other words, it adds a new workbook to the Workbooks collection. After you execute this macro, a fresh workbook is the active workbook.

      Triggering actions with object events

      This section briefly touches on one more topic that you need to know about: events. Events are specific things that you do inside Excel that Excel is constantly listening for (such as switching sheets or changing cells). For example, when you type a value into a cell, a Worksheet Change event occurs. You could, for example, have a VBA macro that is designed to execute whenever someone changes the value of a particular cell.

      This chapter introduces the wonderful world of objects, properties, methods, and events. You find out more about these concepts in the chapters that follow. If you just can’t get enough, you might also be interested in these three excellent tools:

       VBA’s Help system

       The СКАЧАТЬ