vba in-built function

3
 133 Chapter 9: Using VBA and Worksheet Functions 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 func- tions, which I share with you in Table 9-2. I omitted some of the more special- ized 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 VBA’s Most Useful Built-In Functions Function What It Does  Abs Returns a number’s absolute value. Array Returns a variant containing an array. Asc Converts the first character of a string to its ASCII value. Atn Returns the arctangent of a number. Choose Returns a value from a list of items. Chr Converts an ANSI value to a string. Cos Returns a numbers cosine. CurDir Returns the current path. Date Returns the current system date. DateAdd Returns a dat e t o which a sp ecified time interval has been added — for example, one month from a particular date. DateDiff Returns an in te ge r showin g t he number of s pe ci fi ed ti me intervals between two dates — for example, the number of months between now and your birthday. DatePar t Returns an i nt eger contain ing the s pecifie d p ar t o f a gi ven date — for example, a date’s day of the year. DateSerial Conver ts a date to a se rial number. Da te Va lu e Co nver ts a st ri ng to a da te . Day Returns the day of the month from a date value. Dir Returns the name of a file or directory that matches a pattern. Erl Returns the line number that caused an error. Err Returns the error number of an error condition. Error Returns t he error messa ge t hat c orresponds to an error number. Exp Returns the base of the natural logarithm (e) raised to a power. (continued)

Upload: dawncpain

Post on 04-Oct-2015

6 views

Category:

Documents


0 download

DESCRIPTION

vba inbuilt function

TRANSCRIPT

  • 133 Chapter 9: Using VBA and Worksheet Functions

    Discovering VBA functionsHow 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 func-tions, which I share with you in Table 9-2. I omitted some of the more special-ized 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 VBAs Most Useful Built-In FunctionsFunction What It Does

    Abs Returns a numbers absolute value.

    Array Returns a variant containing an array.

    Asc Converts the first character of a string to its ASCII value.

    Atn Returns the arctangent of a number.

    Choose Returns a value from a list of items.

    Chr Converts an ANSI value to a string.

    Cos Returns a numbers cosine.

    CurDir Returns the current path.

    Date Returns the current system date.

    DateAdd Returns a date to which a specified time interval has been added for example, one month from a particular date.

    DateDiff Returns an integer showing the number of specified time intervals between two dates for example, the number of months between now and your birthday.

    DatePart Returns an integer containing the specified part of a given date for example, a dates day of the year.

    DateSerial Converts a date to a serial number.

    DateValue Converts a string to a date.

    Day Returns the day of the month from a date value.

    Dir Returns the name of a file or directory that matches a pattern.

    Erl Returns the line number that caused an error.

    Err Returns the error number of an error condition.

    Error Returns the error message that corresponds to an error number.

    Exp Returns the base of the natural logarithm (e) raised to a power.(continued)

    15_503690-ch09.indd 13315_503690-ch09.indd 133 4/12/10 11:26 PM4/12/10 11:26 PM

  • 134 Part III: Programming Concepts

    Table 9-2 (continued)Function What It Does

    FileLen Returns the number of bytes in a file.

    Fix Returns a numbers integer portion.

    Format Displays an expression in a particular format.

    GetSetting Returns a value from the Windows registry.

    Hex Converts from decimal to hexadecimal.

    Hour Returns the hours portion of a time.

    InputBox Displays a box to prompt a user for input.

    InStr Returns the position of a string within another string.

    Int Returns the integer portion of a number.

    IPmt Returns the interest payment for an annuity or loan.

    IsArray Returns True if a variable is an array.

    IsDate Returns True if an expression is a date.

    IsEmpty Returns True if a variable has not been initialized.

    IsError Returns True if an expression is an error value.

    IsMissing Returns True if an optional argument was not passed to a procedure.

    IsNull Returns True if an expression contains no valid data.

    IsNumeric Returns True if an expression can be evaluated as a number.

    IsObject Returns True if an expression references an OLE Automation object.

    LBound Returns the smallest subscript for a dimension of an array.

    LCase Returns a string converted to lowercase.

    Left Returns a specified number of characters from the left of a string.

    Len Returns the number of characters in a string.

    Log Returns the natural logarithm of a number to base.

    LTrim Returns a copy of a string, with any leading spaces removed.

    Mid Returns a specified number of characters from a string.

    Minute Returns the minutes portion of a time value.

    Month Returns the month from a date value.

    MsgBox Displays a message box and (optionally) returns a value.

    Now Returns the current system date and time.

    RGB Returns a numeric RGB value representing a color.

    15_503690-ch09.indd 13415_503690-ch09.indd 134 4/12/10 11:26 PM4/12/10 11:26 PM

  • 135 Chapter 9: Using VBA and Worksheet Functions

    Function What It Does

    Replace Replaces a substring in a string with another substring.

    Right Returns a specified number of characters from the right of a string.

    Rnd Returns a random number between 0 and 1.

    RTrim Returns a copy of a string, with any trailing spaces removed.

    Second Returns the seconds portion of a time value.

    Sgn Returns an integer that indicates a numbers sign.

    Shell Runs an executable program.

    Sin Returns a numbers sine.

    Space Returns a string with a specified number of spaces.

    Split Splits a string into parts, using a delimiting character.

    Sqr Returns a numbers square root.

    Str Returns a string representation of a number.

    StrComp Returns a value indicating the result of a string comparison.

    String Returns a repeating character or string.

    Tan Returns a numbers tangent.

    Time Returns the current system time.

    Timer Returns the number of seconds since midnight.

    TimeSerial Returns the time for a specified hour, minute, and second.

    TimeValue Converts a string to a time serial number.

    Trim Returns a string without leading or trailing spaces.

    TypeName Returns a string that describes a variables data type.

    UBound Returns the largest available subscript for an arrays dimension.

    UCase Converts a string to uppercase.

    Val Returns the numbers contained in a string.

    VarType Returns a value indicating a variables subtype.

    Weekday Returns a number representing a day of the week.

    Year Returns the year from a date value.

    Using Worksheet Functions in VBAAlthough VBA offers a decent assortment of built-in functions, you might not always find exactly what you need. Fortunately, you can also use most of Excels worksheet functions in your VBA procedures. The only worksheet func-tions that you cannot use are those that have an equivalent VBA function.

    15_503690-ch09.indd 13515_503690-ch09.indd 135 4/12/10 11:26 PM4/12/10 11:26 PM