The AreaCircle() Function

The following nine steps include detailed instructions that explain how to create the custom function AreaCircle(Radius), which you can use to calculate the area of a circle. You have to pass the radius as a number, a calculated expression, or a cell reference as the argument for the function.

  1. Press Alt+F11 to open the Visual Basic Editor (in all Excel versions).

    In Excel 2003 and previous versions, you can select Tools/Macro/Visual Basic Editor, and in the Excel 2010 and Excel 2007, you can click Visual Basic on the Developer tab.

    Like other typical windows in Windows, the Visual Basic Editor has a title bar, a menu bar, and toolbars. On the left side, you can see two subwindows: the Project Explorer window and the Properties window. The large gray area is the workspace, which is still empty (see Figure 6-1).

    You can see your Excel VBA projects in the Project Explorer (which is similar to the folder view in Windows Explorer). Each open Excel workbook has a folder with the name VBAProject followed by the name of the workbook in parentheses, such as Geometry.xlsm or Book1 if the workbook wasn’t saved yet. By default, the Microsoft Excel Objects subfolder contains the objects ThisWorkbook, Sheet1, Sheet2, and Sheet3. (The view of the Project Explorer in versions of Excel before Excel 2003 is slightly different.) The number and names of the subfolders vary depending on your project. In the next step, you will add a new subfolder named Modules that contains the object Module1.

    The Properties window shows the properties and values of the selected VBA object. For the following practice, you don’t need to understand the functionality of the Project Explorer and the Properties window.

    The Visual Basic Editor’s opening screen (Excel 2010).

    Figure 6-1. The Visual Basic Editor’s opening screen (Excel 2010).

  2. Select Insert/Module to prepare the workspace for the command code (see Figure 6-2).

    If you are familiar with VBA, you know that there are other methods by which you can enter VBA procedures. These methods, however, are not suitable for custom functions.

    The Visual Basic Editor with a new but still empty module.

    Figure 6-2. The Visual Basic Editor with a new but still empty module.

  3. Select Insert/Procedure to open the dialog box shown in Figure 6-3, in which you can select the settings for the new function.

    The Add Procedure dialog box with the default settings.

    Figure 6-3. The Add Procedure dialog box with the default settings.

  4. In the Add Procedure dialog box, enter AreaCircle in the Name field and select the Function option. You don’t need to change any of the other options (see Figure 6-4).

    The Add Procedure dialog box with the information required for the function declaration.

    Figure 6-4. The Add Procedure dialog box with the information required for the function declaration.

  5. Click OK to add the procedure. You will see two lines in the workspace, with the cursor in an empty line. The Public keyword allows the module to be used in other modules. This is not necessary in this case, but for simplicity leave it there.

    Public Function AreaCircle()
    |
    End Function
  6. The AreaCircle function has a single argument, the radius. You need to declare the argument for this function in the procedure header. First enter the name of the argument. You can choose any name, but it should be descriptive, because this name will be displayed in the Function Wizard when you use the function in your Excel workbook. The argument type should also be declared. In this case, use As Double, indicating that the data type of the argument is Double, which is the standard number format of Excel worksheets. (This format, which is more precisely a floating-point number with double precision, is saved in 8 bytes in the range –1.79769313486231x10308 to –4.94065645841247x10–324 for negative values and 2.2250738585072x10–308 to 1.79769313486232x10308 for positive values.) Various other data types are available, including Variant, which will be explained later in this chapter.

    Public Function AreaCircle(Radius As Double)
    |
    End Function

    Tip: Add a term to an expression

    If you use keywords within your code, the Visual Basic Editor assists you by displaying relevant options as you type the code. Select a term in the list box and instead of pressing the Enter key, which will move to the next line and generate an error, press the Tab key or Ctrl+Enter to add the term to the expression. You can also ignore the list box and just keep typing.

  7. It is also a good practice to assign a data type to the return value, which is the result of the function; otherwise, the Visual Basic Editor uses the Variant type by default. The Variant data type provides more flexible handling of data types but uses more memory and reduces the processing speed. Because our example uses only the standard Excel number format, enter As Double after the closing parenthesis.

    Public Function AreaCircle(Radius As Double) As Double
    |
    End Function
  8. Enter the actual procedure between these two lines; in other words, the executable code, as shown here.

    Public Function AreaCircle(Radius As Double) As Double
    AreaCircle = 3.14159265358979 * Radius ^ 2
    End Function
  9. The function calculates pi times the radius squared. Pi has been entered as a fixed constant. Press the Tab key to indent the code. This blocks and groups the lines of code and makes reading and understanding the procedure easier.

You have declared the custom AreaCircle() function with the argument Radius. To summarize:

  • In step 4, you created the frame for the function block.

  • In steps 5 through 7, you completed the function declaration.

  • In step 8, you added the program code to the function. The function takes the number from the cell in the worksheet and assigns this to the Radius argument. It then passes the value over to the procedure, in which the area of the circle is calculated by the program code. The result is assigned to the function name with the equal sign. When you use this function name in the worksheet, the result is inserted into the cell by the program code and can be used for other operations. The function can be used repeatedly from anywhere in the workbook.

  • You entered pi as a fixed number. We will deal with another possibility later in the AreaCircle1() example.

The completed AreaCircle() function appears in in the Visual Basic Editor as shown in Figure 6-5.

Tip: Use the apostrophe to indicate a comment

If you enter an apostrophe at any point in a line of code, the text that follows is considered to be a comment and is ignored by the Visual Basic Editor in terms of processing. You can enter comments to explain a particular code section.

Note that the text between the apostrophe and the end of the statement line should not include any executable expressions.

The Visual Basic Editor with the AreaCircle() procedure.

Figure 6-5. The Visual Basic Editor with the AreaCircle() procedure.

You probably want to know whether this function works. To test the function, switch back to the sheet view of Excel—use the taskbar or press Alt+F11. You can close the Visual Basic Editor without saving because the VBA module is part of your workbook and is saved together with the workbook.

  1. Enter a number value for the radius into a cell (for example, enter 2 in cell A1).

  2. Calculate the area of a circle with radius 2. Type in the formula

    =AreaCircle(A1)

    in another cell.

After you entered the formula, the result (the area of a circle with the radius specified in cell A1) 12.5663706 appears.

Every time you change the value in the cell referenced in the AreaCircle() function, Excel recalculates the sheet and therefore the function. The custom function behaves exactly like any built-in Excel function.

Custom functions and their arguments are also listed alphabetically in the User Defined category and can be accessed from there:

  • In Excel 2007 and Excel 2010: On the formula tab, click the Add Function button (the fx symbol).

  • In Excel 2002 and Excel 2003: Select AutoSum/More Functions.

  • In Excel 2000 and earlier: In the Function Wizard, click the fx symbol.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.181