VBA Procedures Subroutines and Functions

About Function Procedures ■ Creating a Function Procedure ■ Executing a Function Procedure ■ Passing Arguments ■ Specifying Argument Types ■ Passing Arguments by Reference and by Value ■ Using Optional Arguments ■ Locating Built-in Functions ■ Using the MsgBox Function ■ Using the InputBox Function ■ Using the InputBox Method ■ Using Master Procedures and Subprocedures ■ What's Next...

In Chapter 2 you learned that a procedure is a group of instructions that allows you to accomplish specific tasks when your program runs. VBA has three types of procedures:

■ Subroutine procedures (subroutines) perform some useful tasks but don't return any values. They begin with the keyword Sub and end with the keywords End Sub. Subroutines can be recorded with the macro recorder (as you did in Chapter 1) or written from scratch in the Visual Basic Editor window (see Chapters 2 and 3). In Chapter 1, you learned various ways to execute this type of procedure.

■ Function procedures (functions) perform specific tasks that return values. They begin with the keyword Function and end with the keywords End Function. In this chapter, you will create your first function procedure. Function procedures can be executed from a subroutine or accessed from a worksheet just like any Excel built-in function.

■ Property procedures are used with custom objects. With property procedures you can set and get the value of an object's property or set a reference to an object. You will learn how to create custom objects and use property procedures in Chapter 11.

In this chapter, you will learn how to create and execute custom functions. In addition, you find out how variables (see Chapter 3) are used in passing values to subroutines and functions. Later in the chapter, you will take a thorough look at the two most useful VBA functions: MsgBox and InputBox.

0 0

Post a comment