Introduction to VBA (and VBScript)

Visual Basic for Applications (VBA) is a subset of the Visual Basic Programming language for use with Microsoft Word, Excel, Access etc. While it contains a many features not supported by VBScript (another subset of VB) , the basic syntax, or construction of the language, is very similar.

Variables

A variable is a virtual container in the computer’s memory that’s used to hold information. In concept, it is much the same as a notepad. You can jot down information on the page of a notepad and return to that specific page later to remember what you wrote or modify the information. A computer program can store information in a variable and then access that information later by referring to the variable’s name.

How Do I Create a Variable?

When you create a variable, you have to give it a name. That way, when you need to find out what’s contained in the variable, you use its name to let the computer know which variable you are referring to. You have two ways to create a variable. For example, you want to create a variable called Quantity, you would enter:

Dim Quantity

'To assign a value to the variable quantity use:
Quantity = 5

Visual Basic for Applications Features Not In VBScript

CategoryOmitted Feature/Keyword
Array HandlingOption Base
Declaring arrays with lower bound <> 0
CollectionAdd, Count, Item, Remove
Access to collections using ! character (e.g., MyCollection!Foo)
Conditional Compilation#Const
#If…Then…#Else
Control FlowDoEvents
GoSub…Return, GoTo
On Error GoTo On…GoSub, On…GoTo
Line numbers, Line labels
ConversionCVar, CVDate Str, Val
Data TypesAll intrinsic data types except Variant Type…End Type
Date/TimeDate statement, Time statement
DDELinkExecute, LinkPoke, LinkRequest, LinkSend
DebuggingDebug.Print
End, Stop
DeclarationDeclare (for declaring DLLs)
Optional
ParamArray
Static
Error HandlingErl
Error
Resume, Resume Next
File Input/OutputAll traditional Basic file I/O
FinancialAll financial functions
Object ManipulationTypeOf
ObjectsClipboard
Collection
OperatorsLike
OptionsDeftype
Option Base
Option Compare
Option Private Module
Select CaseExpressions containing Is keyword or any comparison operators Expressions containing a range of values using the To keyword.
StringsFixed-length strings
LSet, RSet
Mid Statement
StrConv
Using ObjectsCollection access using !

Contact Us