0% found this document useful (0 votes)
9 views

Lecture 19 - Visual Basic

The document provides an introduction to Microsoft Visual Basic, focusing on its use for writing macros in Excel. It explains fundamental concepts such as objects, collections, methods, properties, and events, along with practical examples of how to reference and manipulate Excel sheets and cells using Visual Basic. Additionally, it covers variables, arrays, and the importance of declaring variable types for effective programming.

Uploaded by

derpydiane101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 19 - Visual Basic

The document provides an introduction to Microsoft Visual Basic, focusing on its use for writing macros in Excel. It explains fundamental concepts such as objects, collections, methods, properties, and events, along with practical examples of how to reference and manipulate Excel sheets and cells using Visual Basic. Additionally, it covers variables, arrays, and the importance of declaring variable types for effective programming.

Uploaded by

derpydiane101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Microsoft Visual Basic

Introduction
We’ve seen some brief examples of Visual Basic in our previous lectures.
Visual Basic is the programming language used for writing macros. If you record a macro
like we learned in Lecture 17, the code behind the macro is Visual Basic.
In the Visual Basic lectures, we will cover some of the commonly-used features of the
language.
Because it is learning a whole new language, don’t be surprised if you find that you need to
go through the Lecture multiple times, or re-read sections to understand.

The Developer Tab


I’ve given you a visual basic example file (this already has several macros – as you'll see the
code has to be very precisely written):

1. Open the vba.xlsm file, found on Blackboard.

We’re going to use the Developer tab in the Ribbon to access our macros.
There is a Macros button on the View tab, but it's easier to use the set of commands from the
Developer tab ribbon.

Objects & Collections


Before we look at the macros from the example file, I want you to understand the structures
used in Visual Basic programming.
The fundamental building blocks in Visual Basic are called Objects. The most frequently used
objects in Excel are Workbook, Worksheet, Sheet, and Range.
While a Worksheet object only represents a worksheet, a Sheet can be any type of sheet (such
as a chart sheet).
A Range can refer to either a single cell (example: Range(“A3”)) or range of multiple cells
(example: Range(“A2:D2”).
Other common objects include Charts and PivotTables, while a Collection is a group of objects
of the same class.
Collections provide a more flexible way of working with groups of objects.
A class is like a blueprint. It defines the data and behavior – it’s a construct that enables you to
create custom types by grouping together variables of other types. So a class describes the
variables, properties, procedures and events of objects.
We’ll learn more about variables later in the lecture. 
Workbook and Worksheet Objects
A Workbook is an Excel file and the workbook collection contains all Excel files currently open.
Every workbook contains at least one Worksheet, and the worksheet collection consists of all
worksheets in the workbook (excluding chart or macro sheets).
In Visual Basic, or VBA, a worksheet can be referenced directly with its name in quotes, or via a
number index.
 Worksheets(sheetname) – example: Worksheets("Sheet1") is the worksheet named
"Sheet1"
 Worksheets(number) – example: Worksheets(1) is the leftmost worksheet in the
collection

Please note that Worksheets(1) is not necessarily the same sheet as Worksheets("Sheet1")! If
the first tab in the Workbook was called Stuff and the second tab was called Sheet1, then
Worksheets(1) would be referencing Stuff, not Sheet1.

What if the sheet you want to reference is not a Worksheet? Then you’d use Sheets instead of
Worksheets.
Sheets is a collection of worksheets, chart sheets and macro sheets. Sheets can be indexed the
same way we used worksheets:

 Sheets(1) is the leftmost sheet, no matter what type of sheet it is.


 Sheets(“Stuff”) would be the tab named Stuff.

You can also use ActiveSheet to refer to a sheet that you are actively in at that point in the
code (you can also use ActiveSheet.Next for the sheet one to the right of the current sheet, and
ActiveSheet.Previous for one sheet to the left of the current sheet).

To refer to sheets (or other objects) with the same name (like when you have more than one
Workbook open), you have to name it precisely.

For example:
 Workbooks("Book1").Worksheets("Sheet1")

 Workbooks(2).Worksheets("Sheet1")

If the object is not named specifically, whatever object (such as the workbook or worksheet) is
currently active is used.

Methods, Properties and Events


Each object can have its own methods, properties and events.
A Method is an action performed on an object.
A Property is a built-in (or user-defined) characteristic of the object.
You can program an Event to occur when something happens – like when a workbook is
created or opened, another sheet is activated, or a PivotTable is created.
Events fall out of the realm of commonly used as they are used by more advanced
programmers – so we will not deal with those in this class – but it is important to know what
they are.

The following are examples of some methods:

 Workbooks.Close - closes the active workbook


 ActiveSheet.Delete – to delete the active worksheet (the dialog box is displayed)
 Worksheets("Sheet5").Delete – deletes Sheet5 (but doesn't change which sheet is
active unless Sheet5 was the active sheet)
 Worksheets("Sheet3").Copy – copies Sheet3 to a new workbook
 ActiveSheet.Copy After="Sheet3" – copies active worksheet and places it after
Sheet3
 Sheets("Chart3").Activate – makes Chart3 the active sheet
 Sheets("Chart1").PrintPreview – previews Chart1 ( and .PrintOut – prints out the
chart)
 Worksheets.Add – creates a new worksheet, which is then activated
 Range("B1").Select – selects cell B1
 Worksheets(1).Columns("A:C").AutoFit – autofits columns A, B and C on first sheet
 ActiveSheet.Range("A1:R8").Sort – sorts A1 to R8 on active sheet using values in
column A

And here are some examples of properties:

 Worksheets.Count – tells you how many worksheets are currently open (this can also
be used for Workbooks)
 Range("A1").Value = 30 – puts a value of 30 in cell A1
 Range("A1") = 10 – the default property for a Range is Value, so you don’t have to
use .Value like in the previous example.
 Selection.Value = "Profit" – puts the Word Profit in the active cell (Just like the
previous example, the .Value is optional and this can also be written as
Selection = “Profit”)
 ActiveSheet.Name = "Results" – renames the active sheet as Results

Absolute and Relative Referencing


There are two ways of referencing in VBA - absolute and relative – we learned a bit about this
in Lecture 17 and 18 when creating macros.

Absolute reference examples:


o Range("A1").Select – an absolute reference which makes cell A1 the active cell
o Range("A2:A10") = Range("A1") * 10 – stores ten times A1’s value in cells A2 to
A10
o Cells(1,3) = "hello" – puts hello in cell C1; Cells(row,column) can also be used

Relative references can have two components.


First, there's an offset which is denoted using the number of rows and columns away from the
active cell. Only positive values are allowed.

 ActiveCell.Offset(0, 1) = "x" – this puts an x in the cell to the right of the active cell
(0 rows, 1 column)
 ActiveCell.Offset(1, 0).Select – this selects the cell below the current active cell
(1 row, 0 columns)
 ActiveCell.Offset(0,1) = ActiveCell * 2 – puts twice the active cell in the cell to right
(So if you are in cell A1 and the value in the cell is 5, this code would put a value of 10
in cell B1, which is zero rows and one column from A1).

Second, you can also select a range relative to the offset.


With this, Range("A1") denotes the offset cell, not the cell in the first row of the left column of
the whole worksheet. This can be confusing, but you soon get used to it.

ActiveCell.Offset(3,0).Range("B1").Select – selects the cell in the column to the right


(column B), three rows down
Now try out the first macro in the example file and see if you can understand what's happening
at each stage:
2. On the Developer tab, click on the [Macros] button
3. Select MacroA then click on [Edit]

This will take you into the Visual Basic Editor.


Before running the macro, decrease the size of the window so that you can see the Excel
worksheet at the same time.

4. Maximize the inner code window, if necessary, and Close the two panes on the left
5. Using the mouse, point to the far left of the Visual Basic Editor (VBE) window and drag it
to the right so that it occupies only half the screen (if the window is maximized, you'll
need to click on Restore Down first)
6. Now run the macro by pressing [F8] - this steps through the lines one at a time (the
arrow and yellow highlight indicates where the macro has reached)
7. Repeat step 6 until the last statement (End Sub) is no longer highlighted

Make sure you understand the statements as they are performed. You will notice that there is
text displayed in green that is preceded by an apostrophe. In Visual Basic, text typed after an
apostrophe is held as a comment and is not run as part of the code – this is a helpful way to
add description to the code so anyone else looking at it knows what the code is for!

You can repeat the macro a second time by pressing [F8] again, if you need.
To see a demonstration of walking through code, watch the first part of the Lecture
19 video.

So what is happening in MacroA? Let’s analyze it, one line at a time:

Range("A2").Select –
Range is a cell or group of cells, and Select says “go there” or “choose this” - so in this case,
cell A2 would be selected.

Selection = "values:"
This tells the code to add text (it’s contained in quotes) to the selection (cell A2), so now cell A2
will contain “values:” (without the quotes)

ActiveCell.Next = 10
This says to take the active cell (cell A2) and put a 10 in the next cell, which is one cell to the
right (cell B2).

ActiveCell.Offset(0, 2) = 20
This says to take the active cell (Note that we did not have the code move to the Next cell, just
put 10 in it, so the active cell is still A2!) and put 20 in the cell zero rows and 2 columns away
(cell C2 – and, since we did not select that cell, cell A2 is still the active cell!)

Cells(2, 4).Formula = "=sum(B2:C2)"


This adds the formula =SUM(B2:C2) to a cell. The cell is in the second row and fourth column,
so it is cell D2.

Cells(3, 1) = "here"
This puts the word here in a cell. The cell is in the third row and first column, so it is cell A3.

ActiveCell.Range("B2") = "in b3"


This puts the text phrase ‘in b3’ into cell B3. Why B3 when the code says Range(“B2”)? Well,
remember that Range in VBA is an object, and ActiveCell is a property.
When the property is applied to the object, then the property is relative to the object – and the
property is considered ‘one’.
So relatively, it puts the text into the cell that is “B2” away - one column (property column is
one, B is the second column) and one row (property row is one, 2 would be another row away)
from the active cell. Since the active cell is A2, one row and one column away is B3.
To see another example of this, after this line of code, add the following two lines of code:
Range("C3").Select
Selection.Range("B1") = "is it d3?"
This will now select cell C3, making it the active cell, then put the phrase ‘is it d3’ in a cell that
is “B1” relative to C3 – one column away (current column is always considered one) and zero
rows (current row is always considered one) from the selected cell. So what cell is it? It IS D3.

Range("B2:C2").Select
This selects cells B2 and C2.
Selection = 100
This puts 100 in both cells – B2 and C2.

Selection.Delete
This deletes cells B2 and C2, moving up the cells below it. You will now have a #REF error
message to appear in D2. It would have been better to use Selection.Clear.

Range("A2:D2,A3").Clear
This clears the data from cells A2, B2, C2, D2 and A3.

ActiveCell.Previous.Select
This moves active cell back a column (since B and C were selected, it moves to A, so A2 is now
the ActiveCell.

ActiveCell.Offset(-1, 0).Select
This moves the active cell minus one row (from 2 to 1) and zero columns – so the active cell
now becomes A1.

End Sub
This ends the code!

Variables and Arrays


Visual Basic programming makes wide use of variables and arrays.
A variable is like a cookie jar – a place to store ‘stuff’. In Excel, it can be thought of as the
equivalent of a cell on a worksheet - it can store data (and that data can be replaced by new
values, as can happen if you change the data in an Excel cell).
An array is the equivalent of a range of cells (a row, column or block) and is essentially a set of
variables.
Just as ranges of cells can be named (as covered in the Lecture 3 video), variables and arrays
are also given names (please note that spaces are not allowed in a variable or array name).

Defining Variable/Array Types


Any type of data can be held in a variable, so technically it could hold a number at one point
and then text at another.

However, it is not recommended to do this – instead, the type of variable should be declared
explicitly. Declaring a variable is defining in Visual Basic what type of data a variable will be
used to hold. A variable or array can be restricted to hold certain kinds of data, much the same
way as formatting can be applied to a cell.

This is done through a Dim (short for Dimension) statement.

Dim Statement examples:

Dim x As String ' variable x is for text


Dim A2 As String ' variable names can include a number

Dim i, j As Integer ' variables i and j are for whole numbers

Dim birthday As Date ' variable birthday is for a date

Dim y As Double ' variable y is for double-precision numbers

Dim xx as variant ' any sort of data can be held in variable xx

An array is a simple way to define a whole series of variables using just one name. When you
define the array, you declare the number of items that make up the array. This designates the
array bounds. Arrays can be one-dimensional, like a row or column of cells would be, or two-
dimensional, like a block of cells would be. Technically, arrays can also have more than two
dimensions, though we won’t go that far in our class lectures.

Array DIM statement examples:

Dim x(10) as String ' defines 10 variables called x(1), x(2), x(3) ... x(10) for storing
text

Dim m(2,2) as Single ' defines 4 variables called m(1,1), m(1,2), m(2,1) and m(2,2) for
numbers (like a cube)

Dim z(5 to 9) as Date ' defines 5 variables z(5), z(6) ... z(9) for storing dates

Note** By default, array bounds start at zero. So in the first example above, there is also an
x(0). In the second example, there are 5 extra elements – m(0,0), m(0,1), m(0,2), m(1,0) and
m(2,0). The explicit declaration in the third example, however, means that only these
elements exist.
You don't have to use extra (0) elements if you don't want - and you can define a lower bound
of 1 using an Option Base 1 statement at the very top of your VBA code (using the statement
sets the default for all the macros in the file so none of the arrays will use (0) elements).

Variant Arrays
Sometimes, the data held in rows, columns or blocks of cells are of differing types (text,
numbers, dates, etc.) or even some unknown types. If this is the case, you can define an array
as a variant array.

Variant Array DIM statement examples:

Dim myrow(3) as Variant ' myrow is an array of 3 values

myrow(1) = 5 ' a number is stored in myrow(1)

myrow(2) = "Yes" ' text is stored in myrow(2)

myrow(3) = "31/Dec/1999" ' a date is stored in myrow(3)


The Array function can be used to supply data to a variant array. The following example first
declares the variable as variant, then defines it as an array containing the specified values.

Dim players As Variant

players = Array("Ron", "Dan", "Fred", "Tim", "Chris", "Paul", _

"Jon", "Jamie", "Sam", "Pete")

MsgBox(players(3)) ' displays Tim – Ron is in players(0)

Note** the underscore at the end of the second line in this example is used to indicate that the
statement continues onto another line.

Let’s see how variables and arrays work by running MacroB.

8. The VBA window should still be displayed – if not, click on the Macros button and Edit
MacroB
9. Click on MacroB then use [F8] to step through the macro

This macro picks up the values from an array (x) which is declared as variant so that it can hold
any types of data. Meanwhile, the variable (w) can only hold whole numbers because it is
declared as an integer.
Note** If a variable is used or declared within a macro, the value stored in it is only held while
the macro is running. Try running MacroB again:

10. Run the macro again, using [F5] to run it, rather than step through it – you should find
nothing has changed in your spreadsheet – this is because w has no value when the
macro is started.

You CAN retain the value so that it can be used in other macros. To do this, you have to declare
it outside the macros. Just like earlier when we talked about the Option Base 1, you can declare
globally at the top of your Visual Basic module.

11. Add an apostrophe before the DIM w as Integer statement in the macro (to turn it
into a comment)
12. Using the scroll bar, move to the very top of the VBA display and delete the apostrophe
from the DIM statement here, to activate it and declare w as a Variant.

I demonstrate steps 11 and 12 for you in the second part of the Lecture 19 video.

13. Click anywhere within MacroB and run it again (using [F5]) – the cell is left empty as
w has now been declared variant (it was set to 0 when declared an integer)
14. Repeat step 13 to run it a second time - this time B1 is set to 10 (to see this, you’ll
need to comment out the following lines by adding an apostrophe before each:
a. Range("A1:B1").Clear
b. Range("A1:D2").Clear
The value is held because w is declared globally in the DIM statement as a variant for all
macros in the file.
Finally, try adding a statement which gets rid of the *zero* element of the array:

15. Uncomment the statement Option Base 1 at the very top of the VBA pane – above the
DIM you uncommented earlier.
16. Click on MacroB then press [F5] to run it

You should find the word data appears in cell A2, and an error message also appears. This
happens because originally data was the value held in array element (0) so when using base 1,
“data” is now held in array element (1) – and we didn’t change the reference when it is used
later.
17. Press [Enter] to enter [Debug] mode, then correct the statement to read
ActiveSheet.Name = x(1)
18. Press [F5] to complete the macro.

Input and Output


Sometimes, when running a macro, a user needs to interact with the program - perhaps to
supply some data or to be told some information. The option of being told information can be
used when developing a macro to test whether the code is running correctly. These interactions
are known as Input (supply some data) and Output (be told information).

Output

The code used to get information from a macro contains the Msgbox statement. This can be
used either to relay a fixed message (such as that the macro has reached a particular point in
the code) or to give the current value of a variable used in the program or data held in a cell.
When a Msgbox statement is run, a dialog box appears on the screen, which must be OK'd by
the user.

Note** Msgbox requires a string argument – non-strings need to be converted using the Cstr
function.

Msgbox examples:

Msgbox("Here") or Msgbox "Here" ' the macro has reached this point

Msgbox(A) ' displays what’s held in string A

Msgbox(Cstr(x)) ' displays the number held in x

Msgbox("x = " & Cstr(x)) ' as above but adds x = (so if x was 10, it would say x=10)

Msgbox(Cstr(Range("C2"))) ' displays the value held in cell C2

Msgbox ActiveSheet.Name ' displays the active sheet's name


The default dialog box simply has an [OK] button - but further buttons can be added by
including a prompt parameter after the message. For example, prompt value 4 gives [Yes] and
[No] buttons. The macro can then detect which response has been chosen, and act
accordingly.

This example displays the dialog box and acts on the result:

If MsgBox("Continue?",4) = vbNo Then End


It shows a message box that says ‘Continue?’ with yes or no buttons. If No is selected, then it
ends.

Input
To supply information to a macro, an Inputbox statement is used. This could take the form of a
reply to a question or a request to supply a data value. The information will then be passed to
the macro by a variable, which can then be used to determine what happens next. Again, a
dialogue box is displayed to which the user must respond.

Range("C1") = Inputbox("Input value for cell C1") ‘ supplies a value to cell C1

x = Inputbox("How many rows of data are there?")

For i = 1 to x 'see LOOPS later in this lecture

Q = Inputbox("Do you want to continue (Yes or No)?")

If Q = "No" Then End 'see IF later in this lecture

Note** When wanting a yes or no answer from a user, it is always valuable to give the user
selection buttons rather than an area for a written prompt, as errors can occur due to case
sensitivity, misspellings, etc. So rather than an InputBox, use a MsgBox for ease of use in
yes/no situations!

MacroC has some input/output statements:

19. Click inside MacroC then use [F8] to run through it.
20. Press [F5] to run it a second time.
21. Scroll up to MacroA and move to the end of the first line that says ‘MacroA Macro
22. Hit Enter to add a line, and add your name into the VBA as a comment.

Loops
By using loops, macros can process large amounts of data in Excel.
Loops are set up around other instructions to carry them out a specific number of times or until
a described condition is reached.
There are three main statement pairs which are used to create loops; each has one statement
marking the start of the loop and the other its end.

 For … Next
 While … Wend
 Do … Loop

For … Next
Perhaps the simplest pair of statements is For … Next.
The For statement sets up a variable which increases in value by 1 each time the loop is
completed.
The Next statement marks the end of the loop, which sends the macro back to the For
statement. This statement also checks whether the loop has exceeded its final value and, if it
has, passes control to the statement following the Next statement.
Normally, the loop variable starts with a value of 1 but it does not have to (for example, you
might want the macro to work from a particular row on a worksheet).
The increase value (step value) is also usually 1; but negative or fractional steps can also be
used.

For…Next examples:

For i = 1 to 10
Cells(i,1) = i
Next i
This example sets the values in cells A1 to A10 equal to 1,2, … 9, 10. The first time, it
sets i = 1, and the cell in row 1, column 1 (A1) to 1. Then Next i sends it back to the
For statement. This time, i = 2 and Cells(2,1) = 2 so A2 = 2, then Next i sends it
looping back to the For statement, etc.

For j = 5 to 8
Range("A"&CStr(j)) = j*j
Next j
This example sets the values in cells A5 to A8 equal to 25, 36, 49 and 64
How? When j = 5, Range("A"&CStr(j)) = j*j would be Range("A"&CStr(5)) = 5*5, so
Range(“A5”) = 25. Remember that CStr converts non-strings to strings – 5 is not a
string but A5 is.

For k = 0 to 9 step 2
Range("A"&CStr(k+1)) = k
Next k
This example sets cell A1 to 0, A3 to 2, A5 to 4, A7 to 6 and A9 to 8.

For m = 10 to 1 step -1
Cells(11-m,1) = Cells(m,2)
Next m
This picks up data from column B, starting in B10 so A1 = B10, A2 = B9, etc.

To jump out of a loop at any time, use an Exit For statement:

For i = 1 To 100
x = InputBox("Enter data")
If x = "#" Then Exit For Else Cells(i, 1) = x
Next i
Msgbox CStr(i-1) & " rows of data"
This shows an input box that says ‘Enter data’. If a # is entered, it exits the loop. (Exit
For). If a # is not entered (Else), it stores the input entry in variable x and then assigns
the variable x value to Cells(i,1). It repeats this 100 times. At the end of 100 or when a
# is entered, causing the loop exit, it shows a message box that indicates how many
rows were entered.

A slightly more complicated form of For… Next is the For Each … Next statement.
This can be used to pick up loop values from a cell range; repeat a loop for each member of an
array; or repeat the loop for each object in a collection (like each cell in a range).
Visual Basic can automatically set up a counter for the loop if none is specified.

For Each…Next Examples:

For Each y In Range(“A1:A5”)


Cells(y,2) = y
Next y
This uses the values stored in cells A1 to A5 to set the values for y.

Dim Test(10) as Array


For Each i in Test
Test(i) = Range("A"&CStr(i))
Next i
This declares an array of 10 called Test, and stores the contents of cells A1 to A10 in
the array.

Dim x as Range
For Each x in Range("A1:A10")
x.ClearFormats
x=0
Next x
This declares range variable (x). Then for each cell in the Range (from A1 to A10), it
clears any formatting from the cell and then sets the cell value to 0.
While… Wend
An alternative form of looping is provided by the While statement.

While…Wend example:

While i < 10
X = Selection
If X = "#" then i = i + 1
ActiveCell.Offset(1, 0).Range("a1").Select
Wend
This loops until it finds 10 # signs in a column.
How? The first time i = 1, which is less than 10. If there is a # in the cell, then i = i + 1
so i = 2. If not then it moves one row and zero columns relative (or “A1” away) from
the current. Wend sends it back to the While statement. If there was no # then i still
equals one because it does not equal 2 until it finds a #.

Do … Loop
Yet another way of looping is provided by the Do statement.

Do…Loop examples:

Do Until j = 999
ActiveCell.Offset(1, 0).Range("a1").Select
j = Selection
Loop
This moves cell to cell in a column until it finds a value of 999.

You could also use this instead of saying Do until j = 999:

Do While j <> 999

An alternative form uses a While or Until statement to mark the end of the loop:

Do
ActiveCell.Offset(1, 0).Range("a1").Select
j = Selection
While j < 999
In this case, while marks the end of the loop (you could also use Until j = 999).

MacroD contains examples of all the different types of loops.

23. Click inside MacroD then use [F8] to run through it


24. Using the mouse, position the cursor over any variable (loop counter) to see its current
value

Click on the File tab, then choose Save As. Save the file to your computer as “Assignment
19.[Your Last Name].xlsm”. In Canvas, upload your assignment document as the response to
Question 1 in the Lecture 19 assessment, then answer the rest of the assessment based on
your file and/or comprehension of the lecture.

You might also like