Script For VBA Lectures
Script For VBA Lectures
Sub hello_world()
MsgBox "hello world"
End Sub
We are going to write a hello world program, this is generally the first program
students learn when writing programs
We will write a Hello World program in Excel using Visual Basic
We begin by navigating to the developer tab on the ribbon and then click Visual
Basic icon on the left side of the screen
Insert > Module >
The VBA editor is ready to accept our program
I will call it hello world
The first word is sub for subroutine
Sub hello-world
The dash is not a good separator, VBA thinks it is a minus sign
VBA colors erroneous code red, and gives us an error message that is not exactly
clear
There is one line to our program: msgbox hello world
We get the red text again and we realize that the statement must have quotation
marks
The program is ready lets execute it
Click the triangle
It runs and now we must save it as a macro enabled file
Save it as a macro enabled file then close it and open it. Then we will run it from
the ribbon, run sub user form
Then click Tools, Macros
It is better to indent things appropriately. We have Sub and End Sub at the first
level with no indentation, next is For and Next indented once at the second level.
Finally, Cells and seq_nbr as the third level.
In Excel VBA you do not have to declare your variables. You can just start using
them.
When you declare your variables you are telling the computer how much space to
allocate for that variable in the computer memory. For example, you would not
need as much space for phone number (13 alpha numeric characters) versus
street address which could be 25 characters or much longer. So, I just explained
why this is good for the computer. It is good for you the programmer because
when you must declare your variables you are protecting yourself from your own
mistakes. You might for example spell a variable name wrong the second time
you use it and then when you run your program it doesn’t just blow up with error
messages but it does not do what you intended because you are storing
information in the wrong variable.
In this first line of the program we establish our control variable by putting a value
of 6 in it
seq_nbr = 6
In regular Excel we call this first cell in the rows and the columns, A1. In Excel VBA
it is called cell 1, 1. It sits in row 1 of column 1.
This line of code essentially assigns a value of 6 to cell 1, 1. The cell is in row 1,
column 1.
Cells(Row, 1).Value = seq_nbr
seq_nbr = seq_nbr + 1
Then we increment the control variable and the program circles back to the top.
As long as the program is operating in the numbers 1 – 10 of your Excel file it will
repeat until it escapes from the program when it reaches row 11.
PERFORMING CALCULATIONS-FORMATTING AND DISPLAYING RESULTS.mp4
A different version of the FOR LOOP program using Negative Step
This is a different version of the FOR LOOP program using Negative Step. We will
learn now to add numbers and display and format the output
Negative Step means we will use our first statement to start at the bottom of our
table, row 7 and we go all the way up to row 3 by using negative step of negative
one, we add numbers together and we display and format the output
Sub add_ALL_expenses()
For Row = 7 To 3 Step -1
SumTotal = SumTotal + Cells(Row, 4).Value
Next
'DISPLAY the word TOTAL in column A
Cells(8, 1).Value = "TOTAL"
Cells(8, 1).Font.Bold = "True"
Cells(8, 1).Font.Name = "Arial"
Cells(8, 1).Font.Size = 14
Cells(8, 1).Font.Color = -16776961 'same as vbRed below
End Sub
The line in green is a comment so that someone reading the program later on will
know what you are trying to do. You create a comment by putting an apostrophe
in front of it.
We don’t declare any variables, we just start using them and the VBA compiler
will decide which ones we need.
The program simply adds all of the numbers in column 3 and puts the sum in
column 5
It is really a simple program but it is written in “computer language” and this
makes it seem odd
Please note I learned the hard way that if you write a program in Word it will not
run in VBA most of the time. Look at this subtle difference the VBA quotation
marks are straight, the quotation marks written in word are slanted and produce
error messages when copied into VBA
Sub add_MANDATORY_expenses_ONLY()
For Row = 7 To 3 Step -1
If Cells(Row, 3).Value = "Mandatory" Then
SumMandatory = SumMandatory + Cells(Row, 4).Value
End If
Next
End Sub
Sub add_OPTIONAL_expeNses_ONLY()
For Row = 7 To 3 Step -1
If Cells(Row, 1).Value > 3 Then
SumOptional = SumOptional + Cells(Row, 4).Value
End If
Next
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub add_ALL_expenses()
current_row = 3
Do While current_row < 8
SumTotal = SumTotal + Cells(current_row, 4).Value
If Cells(current_row, 3).Value = "Mandatory" Then
SumMandatory = SumMandatory + Cells(current_row, 4).Value
Else
SumOptional = SumOptional + Cells(current_row, 4).Value
End If
current_row = current_row + 1
Loop
'DISPLAY TOTAL SUM
'----------------------------------
'DISPLAY the word TOTAL in column A
Cells(8, 1).Value = "TOTAL"
Cells(8, 1).Font.Bold = "True"
Cells(8, 1).Font.Name = "Arial"
Cells(8, 1).Font.Size = 14
Cells(8, 1).Font.Color = -16776961 'same as vbRed below
'DISPLAY the sum for ALL expenses in column D
Cells(8, 4).Value = SumTotal
Cells(8, 4).Style = "CURRENCY"
Cells(8, 4).Font.Bold = "True"
Cells(8, 4).Font.Name = "Arial"
Cells(8, 4).Font.Size = 14
Cells(8, 4).Font.Color = vbRed
Cells(8, 4).Interior.ColorIndex = 27
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Conditional Statements – More Formatting
The reason I did not save my original program it was not an oversite. I plan to add more subroutines to
original things and additional things. The reason is that the original program added all expenses
together. Now I would like to separate them, to add all mandatory expenses. Let’s add the new
program, what does it do? Let’s go to row to row 7 I go all the way up I will summarize all columns that
have expenses type mandatory. Which column is it, column C or column 3. I add numbers in column 4
but only if in column 3 I find mandatory. How to check for mandatory? I use a conditional statement,
that check for the value of the cell mandatory. If I compare it with the literal, I have to put the quotation
marks. Or I can color it Font.ColorIndex = 3 which is red
I use a different way to color it red I use RGB, 255 zero zero
So that was adding Mandatory expenses now I will copy Optional expenses.
The first thing that needs to be done is I check if the column 1 sequence number is more than 3 the type
of expense is optional. So if I compare with a number I do not need to use quotation marks. So we
wrote three separate programs. One adds all numbers together, the second one adds only mandatory
expenses, the third adds only optional expenses. Let’s execute all three of them. I click my run program
and it only does the last item. (See the word optional, there on the left) Why because it does the last
active program. To make sure it does what we want I got to tools macros and I select the second
program MANDATORY Expenses, run and then I go again. I select Tools, Program and I select
Sub add_ALL_expenses()
For Row = 7 To 3 Step -1
SumTotal = SumTotal + Cells(Row, 4).Value
Next
'DISPLAY the word TOTAL in column A
Cells(8, 1).Value = "TOTAL"
Cells(8, 1).Font.Bold = "True"
Cells(8, 1).Font.Name = "Arial"
Cells(8, 1).Font.Size = 14
Cells(8, 1).Font.Color = -16776961 'same as vbRed below
End Sub
Sub add_MANDATORY_expenses_ONLY()
For Row = 7 To 3 Step -1
If Cells (Row , 3).Value = "MANDITORY" Then
SumMandatory = SumMandatory + Cells(Row, 4).Value
End If
Next
'DISPLAY the word MANDATORY in column A
Cells(10, 1).Value = "Mandatory"
Cells(10, 1).Font.Italic = "True"
Cells(10, 1).Font.Name = "Batang"
Cells10, 1).Font.Size = 16
Cells(10, 1).Font.Color = RGB(255, 0, 0) 'same as vbRed below
End Sub
Sub add_OPTIONAL_expenses_ONLY()
If Row = 7 To 3 Step -1
If Cells (Row , 1).Value > 3 Then
SumOptional = SumOptional + Cells(Row, 4).Value
End If
Next
'DISPLAY the word OPTIONAL in column A
Cells(12, 1).Value = "Optional"
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Before I talk to you about the differences in the two modules I want to talk to you about the different
colors here.
In the FOR loop. You see the first_row is = 3 and current column is equal to 2, these are the numbers we
will be working with. This program is controlled by the control variable Current_row and I set it equal to
first_row
This command goes to the bottom of your Excel worksheet, then goes up looking of the first nonblank
cell. When it finds it this row is considered to be the last row of your data file. Then I use my For
…..Next loop command. What ever you find between the For and Next is considered to be the body of
the loop. The For is controlled by the first_row which we initially initialized as 3, then we go to the last
row which we found out in this line. Everytime we hit the Next statement we increment the control
variable “current_row” each time the program goes back to the beginning of the loop it checks whether
you are still between the first row and the last row. If not I escape from the loop.
I have declared my variable with the “Dimension” command. I declare it as long which means an integer
variable. This tells the computer how much space to give the variable in memory.
Current_row = current_row + 1
Then we have the loop statement automatically sends us to the Do While and here we check again the
boundaries, are we still between current_row and last_row if so we performs the Do While otherwise it
goes out and displays the totals
In the Do While loop we have to declare our variables and also increment the control variable in the For
Next loop the control variable is incremented by the Next statement