SoftwareGuide_VBA
SoftwareGuide_VBA
Variable Definitions:
Unlike some of the other languages utilized throughout the ENED1100/1120 course sequence,
variables must be defined before they are able to be used in VBA. There are four main types of
variables that you will likely use: Double, Integer, String, and Boolean.
Arithmetic Operations:
The basic arithmetic operations are the same in VBA as they are in most programming languages:
+ addition
− subtraction
* multiplication
/ floating point division
\ integer division
Mod modulus (remainder of division)
^ power
These operations also follow the same order of operations as found in other languages:
• Operations inside parentheses
• Power
• Sign
• Multiplication/Floating Point Division
• Integer Division/Modulus
• Addition/Subtraction
VBA also has some of the more complex math functions typically used:
Absolute Value Abs
Square Root Sqr
Trig Functions Sin, Cos, Tan, Atn
Logs and Exp Log, Exp
Rounding Round
Additionally, any function that you might use in an Excel formula can be accessed in VBA using
the following syntax:
WorksheetFunction.FunctionName()
where FunctionName is the name of the Excel command you wish to use.
Comparison and Logical Operators:
There are three main logical operators in VBA:
Operator VB Syntax
And Condition1 And Condition2
Or Condition1 Or Condition2
Not Not Condition
There are six main comparison operators in VBA:
Operator VB Syntax
Equal to Value1 = Value2
Not equal to Value1 <> Value2
Less than Value1 < Value2
Greater than Value1 > Value2
Less than or equal to Value1 <= Value2
Greater than or equal to Value1 >= Value2
Conditional Structures:
The IF structure in Visual Basic takes the following form:
If condition1 Then
Actions
ElseIf condition2 Then
Actions
⁞
Else
Actions
End If
When using the IF structure, you may include as many or as few ElseIf’s as is needed, and the
Else is optional.
For Loops:
The For loop in Visual Basic takes the following form:
For variable = start To end Step change
Actions
Next variable
Where variable is the loop control variable of your choice, start give the initial value, end
gives the final value, and change determines how the loop control variable progresses from
start to end. If the Step change section is left off, Visual Basic will automatically
increment the loop control variable by 1 each time through the loop. Note that your variable
will need to be defined ahead of time.
Do While Loops:
The Do While loop can be set up using either of the following form:
Do While condition Do
Actions Actions
Loop Loop While condition
Like most languages, the Do While loop will continue to repeat as long as the expression defined
in condition is true. When set up as shown on the left, the condition will be checked first
before completing an iteration. When set up as shown on the right, the condition will be checked
after completing an iteration, guaranteeing at least one iteration of the loop before exiting.
Do Until Loops:
The Do Until loop takes the following form:
Do Until condition Do
Actions Actions
Loop Loop Until condition
Unlike the Do While loop, the Do Until loop continues to repeat if the condition is false and exits
once the condition becomes true. However, the Do Until loop can also be set up to either check
the condition prior to an iteration (left) or after an iteration (right).
Arrays:
Arrays can be specified similarly to single variables in Visual Basic, with the exception that a size
of the array is given:
Dim arrayName(n) As Variabletype
Where arrayName is that name you wish to give the array variable, n is the value of the last
index you wish to have in the array (i.e. this will create n+1 spaces in the array), and
Variabletype is the type of value you want to store within the array. Once the array has been
declared, you can store values into the various locations within the array by specifying the index
(starting at 0) of the location at which you’d like to store a value:
arrayName(index) = value
If you wish to initialize the array when it is first declared, you can do so using the following:
Dim arrayName()
arrayName = Array(values)
Notice that when defining the initial values of an array in Visual Basic, you do not need to specify
the size. Visual Basic will automatically create the array of the correct size to store the values.
The size of the array is specified when the array is first defined and cannot be changed, unless the
array is redimensioned:
ReDim arrayName(m)
ReDim Preserve arrayName(m)
In the first case, the array is redimensioned to have a size of m, but all values in the array are
erased. In the second case, the array is redimensioned, but the data is preserved.
Multidimensional arrays can be specified and initialized in much the same was as one
dimensional arrays. A two dimensional array would be defined in the following way:
Dim arrayName(r,c) As Variabletype
As with most languages, the row is specified first when sizing and indexing and the column is
specified second.
There are also many times when it is helpful to determine the size of an array. For this, the
UBound command can be used:
x = UBound(arrayName)
The UBound command will return the last available index in the array.
VBA Functions:
Creating a function in VBA uses the following syntax:
Function FunctionName(args)
Actions
End Function
Where FunctionName is the name you wish to use for your function, args is the list of input
arguments required for your function (separated by commas), and Actions is the set of
commands that define how your function operates.
If you want your function to return a value, you need to assign a value to the name you’ve used
for your function inside the body of the function:
Function MySum(x,y)
MySum = x + y
End Function
In this case, when the function is executed, the value of its two inputs will be added together and
returned. You call the function the same way you would any other function in Excel:
A = 1
B = 2
C = MySum(A,B)
where each of the italicized entries above would be replaced with the requisite code to refer to the
appropriate workbook, worksheet, and cell.
When only working with a single workbook and a single worksheet, you can access a cell using
the command below:
ActiveSheet.Cells(r,c).Value
where r and c are the row and column index in the worksheet as integer numbers (column A is
column 1). ActiveSheet refers to the current worksheet that is visible within Excel. In the
case of a single workbook and worksheet, that will be the only worksheet that can be active.
When working with multiple worksheets in a single workbook, we change the ActiveSheet
command to the Sheets command:
Sheets(Name/Index).Cells(r,c).Value
where Name/Index is either the name of the worksheet (as shown in the tab at the bottom of the
Excel window) or the index of the worksheet (left most worksheet is index 1, next to the right is
index 2, and so on). Remember that the name is a string, and thus should be contained in " ".
When working with multiple workbooks, we add on the workbooks command to refer to the
appropriate workbook:
Workbooks(Name/Index).WorksheetReference.Cells(r,c).value
where Name/Index is either the name of the workbook (i.e. the filename) or the index of the
worksheet (first workbook opened is index 1, next workbook opened is index 2, and so on). For
the WorksheetReference, you can use either the Sheets command or the ActiveSheet
command, depending on your situation.
There may be times when it is useful to identify a specific cell and refer to cells around it. This
can be done using the Offset command:
ActiveSheet.Cells(r,c).Offset(a,b).Value
where r and c are the row and column index in the worksheet of the starting cell and a and b are
the offset in the row and column directions from the starting cell. Note that the values of a and b
can be either positive or negative, depending on where in relation to the starting cell the desired
cell is located.
Move a worksheet:
Sheets.Move Before:=Sheets(Name/Index) OR :=ActiveSheet
Sheets.Move After:=Sheets(Name/Index) OR :=ActiveSheet
Working with Workbooks:
There are a variety of commands that allow you to work with workbooks in Excel:
Make a specific workbook the current active workbook:
Workbooks(Name/Index).Activate
How to open a workbook in the same folder as the currently active workbook:
Cur_WB = ActiveWorkbook.Name
FilePath = ActiveWorkbook.Path
Workbooks.Open Filename:= FilePath & "\name.xlsx"
New_WB = ActiveWorkbook.Name