Sie sind auf Seite 1von 2

VBA Summary

Remember: Use F1 for help and F2 for object listings. Properties are usually set equal to
something, unless they are read-only. Methods can be a Sub or a Function. The parameters of a
Function are always contained within parentheses; the parameters of a Sub are NOT contained
within parentheses. Most of the methods that we will use are Subs. In a parameter list, you do not need
to name the parameter if they are given in the proper order. If you name the parameters, use := to give the
value of the parameter. If you name one parameter, you must name all parameters used.
Object variables are Set
Set rng1 = Range(C2:D5)
Some collections of objects
Worksheets(tabname)
A Function used for input
variable = InputBox(prompt:=something, [title], [default])
Parameters in square brackets are optional. If Cancel is clicked an empty string is returned.
A Sub used for output
MsgBox
Some output here, [buttons], [title]
Use the ampersand & to add strings together.
A Function used for output
response = MsgBox(Some output here, [buttons], [title])
Values for response: vbOk, vbCancel, vbAbort, vbRetry, vbIgnore, vbYes, vbNo.
Some properties of Range that are objects (thus, to use, another dot is necessary)
Range( ).Font
Range( ).Interior
Range( ).NumberFormat =
Range( ).Name =
Range( ).Formula =
Some simple properties of the Font object
Bold, Color, FontStyle, Italic, Size, Strikethrough, Underline
Some simple properties of the Interior object
Color, Pattern, PatternColor

(use F2 for various Pattern styles)

Color is determined either by the RGB function or by a vb constant: vbBlack, vbBlue, vbCyan,
vbGreen, vbMagenta, vbRed, vbWhite, and vbYellow.
The IF statement
strVariable = InputBox(prompt:=something, [title], [default])
If IsNumeric(strVariable) Then
1

do something here for user using the Cancel button


Else
do something else here
End If

Math functions in VBA


Abs
Absolute value
Sqr
Square root
Mod
Modular
Int
Integer value
Rnd
Random number
Exp
e^x
Log
Natural log (base e)
Sin, Cos, Tan, Atn

Abs(-5) 5
Sqr(25) 5
15 Mod 12 3
Int(3.8) 3, Int(-3.1) -4
Exp(1) 2.718282
Log(2.718282) 1

Sine, Cosine, Tangent, and Arctangent of angle in radians

String functions
UCase
returns a string in all upper case characters UCase(Here) HERE
LCase
returns a string in all lower case characters LCase(Here) here
Ltrim
deletes leading blanks
Ltrim( Here ) Here
Rtrim
deletes trailing blanks
Rtrim( Here ) Here
Trim
deletes leading and trailing blanks
Trim( Here ) Here
Left
extracts string Left(here is a string,6) here i
Right
extracts string Right(here is a string,7) string
Mid
extracts string Mid(here is a string,3,7) re is a
Mid(here is a string,3) re is a string
InStr
returns position of text InStr(1,here is a string,is) 6
Len
returns the length of the string
Len(here) 4
IsNumeric Boolean IsNumeric(3.2) True, IsNumeric(3..2) False
Format
returns string
Format(45.3, $0.00) $45.30
Format(45.3, 0.##) 45.3
Format(0.453, 0.00%) 45.30%
Format(4123456.54, #,##0) 4,123,457
Almost all other Excel functions that are not duplicated as a VBA function, can be accessed by the
WorkSheetFunction object; for example, WorkSheetFunction.Average(Range(A1:A10)),
returns the average value from A1:A10 and WorkSheetFunction.RandBetween(1, 9) returns an integer
random number between 1 and 9.

Das könnte Ihnen auch gefallen