You are on page 1of 4

Chapter 9: Using VBA and Worksheet Functions

129

Discovering VBA functions
How do you find out which functions VBA provides? Good question. The best source is the Excel Visual Basic Help system. I compiled a partial list of functions, which I share with you in Table 9-2. I omitted some of the more specialized or obscure functions. For complete details on a particular function, type the function name into a VBA module, move the cursor anywhere in the text, and press F1.

Table 9-2
Function
Abs Array Asc Atn Choose Chr Cos CurDir Date DateAdd DateDiff

VBA’s Most Useful Built-in Functions
What It Does
Returns a number’s absolute value Returns a variant containing an array Converts the first character of a string to its ASCII value Returns the arctangent of a number Returns a value from a list of items Converts an ANSI value to a string Returns a number’s cosine Returns the current path Returns the current system date Returns a date to which a specified time interval has been added — for example, one month from a particular date Returns an integer showing the number of specified time intervals between two dates, — for example, the number of months between now and your birthday Returns an integer containing the specified part of a given date — for example, a date’s day of the year Converts a date to a serial number Converts a string to a date Returns the day of the month from a date value Returns the name of a file or directory that matches a pattern
(continued)

DatePart DateSerial DateValue Day Dir

TEAM LinG

130 Part III: Programming Concepts Table 9-2 (continued) Function Erl Err Error Exp FileLen Fix Format GetSetting Hex Hour InputBox InStr Int IPmt IsArray IsDate IsEmpty IsError IsMissing IsNull IsNumeric IsObject LBound LCase What It Does Returns the line number that caused an error Returns the error number of an error condition Returns the error message that corresponds to an error number Returns the base of the natural logarithm (e) raised to a power Returns the number of bytes in a file Returns a number’s integer portion Displays an expression in a particular format Returns a value from the Windows registry Converts from decimal to hexadecimal Returns the hours portion of a time Displays a box to prompt a user for input Returns the position of a string within another string Returns the integer portion of a number Returns the interest payment for an annuity or loan Returns True if a variable is an array Returns True if an expression is a date Returns True if a variable has not been initialized Returns True if an expression is an error value Returns True if an optional argument was not passed to a procedure Returns True if an expression contains no valid data Returns True if an expression can be evaluated as a number Returns True if an expression references an OLE Automation object Returns the smallest subscript for a dimension of an array Returns a string converted to lowercase TEAM LinG .

Chapter 9: Using VBA and Worksheet Functions 131 Function Left Len Log LTrim Mid Minute Month MsgBox Now RGB Replace Right Rnd RTrim Second Sgn Shell Sin Space Split Sqr Str StrComp String What It Does Returns a specified number of characters from the left of a string Returns the number of characters in a string Returns the natural logarithm of a number to base e Returns a copy of a string. with any trailing spaces removed Returns the seconds portion of a time value Returns an integer that indicates a number’s sign Runs an executable program Returns a number’s sine Returns a string with a specified number of spaces Splits a string into parts. with any leading spaces removed Returns a specified number of characters from a string Returns the minutes portion of a time value Returns the month from a date value Displays a message box and (optionally) returns a value Returns the current system date and time Returns a numeric RGB value representing a color Replaces a substring in a string with another substring Returns a specified number of characters from the right of a string Returns a random number between 0 and 1 Returns a copy of a string. using a delimiting character Returns a number’s square root Returns a string representation of a number Returns a value indicating the result of a string comparison Returns a repeating character or string (continued) TEAM LinG .

The following is an example: Total = Application. (Remember. any statement that uses a worksheet function must use the Application. minute. which is contained in the Application object.Sum(Range(“A1:A12”)) TEAM LinG . Fortunately. WorksheetFunction (with a dot separating the two).WorksheetFunction qualifier. you must precede the function name with Application. and second Converts a string to a time serial number Returns a string without leading or trailing spaces Returns a string that describes a variable’s data type Returns the largest available subscript for an array’s dimension Converts a string to uppercase Returns the numbers contained in a string Returns a value indicating a variable’s subtype Returns a number representing a day of the week Returns the year from a date value Using Worksheet Functions in VBA Although VBA offers a decent assortment of built-in functions. VBA makes Excel’s worksheet functions available through the WorksheetFunction object. the Application object is Excel. The only worksheet functions that you cannot use are those that have an equivalent VBA function.) Therefore. you can also use most of Excel’s worksheet functions in your VBA procedures. you might not always find exactly what you need. In other words.132 Part III: Programming Concepts Table 9-2 (continued) Function Tan Time Timer TimeSerial TimeValue Trim TypeName UBound UCase Val VarType Weekday Year What It Does Returns a number’s tangent Returns the current system time Returns the number of seconds since midnight Returns the time for a specified hour.WorksheetFunction.