|
|
| Back to Excel Homepage | Excel VBA - Reference Guide |
|
Menu Visual Basic Functions
Other links
|
Using VBA is a combination of utilising libraries available, mainly Excel and VBA. The following list of functions is a selection of commonly used functions, which are classes of the VBA library. CONVERSION ClassThis class contains various functions to help cast and convert values from one data type to another. Some functions in this class: CBool(expression) convert to a Boolean (true/false) value CByte(expression) convert to a Byte value CCur(expression) convert to a Currency value CDate(expression) convert to Date value CDbl(expression) convert to a Double value CDec(expression) convert to a Decimal value CInt(expression) convert to an Integer value CLng(expression) convert to a Long value CSng(expression) convert to a Single value CStr(expression) convert to a String value CVar(expression) convert to Variant value The expression can be either a string or numeric
value, which is converted to one of the above data types.
The above example takes a string variable, which the
InputBox
function returns as a string and converts it to an integer value and
stores to the integer, variable. VBA is intelligent enough to convert values without the need to apply conversion functions or explicitly declare variables. However, there are occasions when this rule doesnt work and to handle unforeseen errors, users need to handle data conversion (as above). In future releases of VBA, data type declarations will become more stringent in how users can work with variables and will therefore need to use such functions (as above) to handle cast and conversion issues correctly. DATETIME ClassThis class is a collection of date/time conversions and interrogations.
Some functions in this class: Date return/sets the systems date Now returns/sets the systems date and time Day(Date) returns the day element of the date Month(Date) returns the month element of the date Year(Date) returns the year element of the date DateDiff(interval, date1, date2 [,firstdayofweek] [,firstweekofyear]) returns the difference between two dates driven by the interval DateSerial(Year, Month, Day) returns a valid date from 3 separate values DateValue(Date) converts a string date into a date data type date Weekday(Date, [firstdayofweek])
Returns a string day of the week
INFORMATION ClassThis class is a collection of status functions to help evaluate conditions of variables and objects alike. Some functions in the class: IsArray(variant) returns true or false IsDate(expression) returns true or false IsError(expression) returns true or false IsEmpty(expression) returns true or false IsMissing(variant) returns true or false IsNull(expression) returns true or false IsNumeric(expression) returns true or false IsObject(expression) returns true or false An expression or variant is the variable being tested to see if it is True or False. MATH ClassThis class is a collection of mathematical functions that can be used to change variables with ease and without having to create your own functions. Some functions in the class: Abs(Number) returns the absolute number (always a positive value) Rnd([Number]) returns a random value Round(Number, [NumDigitsAfterDecimal]) returns a rounded value Sqr(Number) returns a square
value (x2)
STRING ClassThis class is a collection of text (string) based functions that include conversion, extractions and concatenation. Some functions in the class: Asc(String) returns the numeric ASCII value of the character string Chr(CharCode) returns the character string from the code supplied Format(Expression, [Format], [FirstDayOfWeek], [FirstWeekOfYear]) - returns the format presentation of the expression InStr([Start], [String1], [String2], [Compare]) returns the numeric position of the first character found from left to right InStrRev(StringCheck, StringMatch, [Start], [Compare]) returns the numeric position of the first character found from right to left LCase(String) returns the string in lowercase UCase(String) returns the string in uppercase Left(String, Length) returns the remaining characters from the left of the length supplied Right(String, Length) returns the remaining characters from the right of the length supplied Len(Expression) returns a value of the length of characters supplied Mid(String, Start, [Length]) returns the portion of characters as determined by the start and end parameters supplied Trim(String) removes unwanted spaces from left and right of the string supplied and extra spaces in between multiple strings
Which Library?Some functions in VBA may appear to be duplicates to functions known in Excel.
The above example uses two functions that appear in both VBA and Excel. By default, VBA is the library used when calling such a function. To use the Round() function from Excels library, users need to call the qualification (object hierarchy) first.
The MsgBox statement and function and InputBox function are members of the Interaction class in the VBA library.
|
Note: The VBA library is the top most reference followed
by the Excel library and both can not be moved or disabled. Therefore, calling a
function which exists in both libraries will always use the VBA reference (as
implicit). VBA Keywords: MsgBox, InpuBox, Application, WorksheetFunction and VBA Class (all functions). |
|
© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved. All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy. |