11 1022q Vba Basics s2018

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

COMP1022Q

Introduction to Computing with Excel VBA

VBA Basics

Gibson Lam and David Rossiter


Outcomes
• After completing this presentation, you are
expected to be able to:
1. Use the MsgBox command to show a
message box using VBA code
2. Understand the idea of using variables
3. Write VBA code with different operators
4. Use the InputBox command to ask for input
from the user

COMP1022Q VBA Basics Page 2


Writing VBA Code
• Previously you have learned that you can write
VBA code in a macro
• You put multiple lines of code inside a
subroutine, with each line of code carrying some
VBA commands
Code runs
• When you run the VBA code …code…
from top to
inside the subroutine the code bottom
is executed from top to bottom inside a
subroutine
sequentially …code…

COMP1022Q VBA Basics Page 3


Using MsgBox
• MsgBox is a useful command to show a few words
in a small window
• Here is an example:

• Here is the VBA code which generates the message:


MsgBox "Welcome to COMP1022Q!"

COMP1022Q VBA Basics Page 4


Changing the MsgBox Appearance
• If you want to, you can change the title and
icon of a message box
• Here is an example:

MsgBox "Welcome to COMP1022Q!" , vbExclamation , "Welcome"

Message text The icon you Title of the


want to use MsgBox window
The Types of Message
Icon The name Description
of the icon
(No icon) vbDefaultButton1 Default display, no icon shown
vbExclamation A warning message icon
vbCritical A critical message icon
vbInformation An information message icon
vbQuestion A query icon

• There are some other icons you can use, but they are not very useful

COMP1022Q VBA Basics Page 6


Using the Underscore ‘_’
• Sometimes a line of VBA code can be very long
so that it becomes very hard to read
• You can break down a single line of code into
multiple lines using an underscore ‘_’, like this:
MsgBox "Welcome to COMP1022Q!", vbExclamation, "Welcome"

Use underscores to put one VBA


command into multiple lines
MsgBox "Welcome to COMP1022Q!", _
vbExclamation, _
"Welcome"
COMP1022Q VBA Basics Page 7
More MsgBox Examples
MsgBox "I lost my mobile phone! :(" , _
vbCritical , _
"Oh no!"

MsgBox "Did you know I just stole all your money?" , _


vbQuestion , _
"Hahaha"
Variables
• You can think of a variable as a box David
• There are different types of box which Name
hold different things
• For example, you can use a box to hold a
number or a piece of text
• You can create as many variables as you like in
VBA code but each variable must be given a
unique name

COMP1022Q VBA Basics Page 9


Making and Using a Variable
• For example, you can make a variable called Name
which holds a string (a piece of text) using this
code: Dim Name As String
• You can use the variable to hold
some text using this code: David
Name = "David" Name

• You can then show the content of


the variable using a message box:
MsgBox Name
Operators
• Previously you have learned some Excel formula
operators
• Operators in VBA are very similar to those you
have used in formulas
• We will have a quick look at the following types
of operator:
– Arithmetic operators
– String operator
– Relational operators
– Logical operators
Arithmetic and String Operators
Operator Description
^ Power
* and / Multiplication and division
Mod Modulus, e.g. 8 Mod 5 gives you 3
+ and - Addition and subtraction
& Putting two strings together, e.g.
"Very" & "Funny" gives you
"VeryFunny"

COMP1022Q VBA Basics Page 12


Relational Operators
• Relational operators return a result of True or
False after comparing two values
Operator Description
= Equal to
<> Not equal to
< Smaller than
<= Smaller than or equal to
> Bigger than
>= Bigger than or equal to

COMP1022Q VBA Basics Page 13


Logical Operators
Operator Description
Not Not returns False if the given
condition is True and returns True
if the condition is False
And Given two conditions, And
returns True if both conditions are
True; otherwise it returns False
Or Given two conditions, Or returns
True if one or both of the
conditions are True; otherwise it
returns False
Using InputBox Input the text here

• InputBox displays
a message box where
you can enter a value
or a message
• The user simply types something, and presses OK
• After that, we need to store what the user has typed,
then we can do something with it, like this:
We store the
result of the Dim Name As String
input box in Name = InputBox("What is your name?")
a variable
COMP1022Q VBA Basics Page 15
Changing the InputBox Appearance
• You can change the title and default text
• The default text is shown when the input box appears,
before the user does anything
• For example: In this
example,
the default
text is
‘David’

• The VBA code:


Default
Dim Name As String text
Name = InputBox( "What is your name?" , _
"Input your name" , "David" )
Using Both InputBox and MsgBox
• We have looked at MsgBox and InputBox
• The following example uses both
• First, it asks for a name using an input box
• Then it shows a conclusion using a message box

COMP1022Q VBA Basics Page 17


Example of InputBox and MsgBox
Sub Example()
This VBA code is created as
a macro called Example()
Dim Name As String

Name = InputBox("What is your name?", _


"Input your name", "David" )
Putting strings
together The default text
MsgBox Name & " is the best name!", , _
"Conclusion"
In this example we don’t
End Sub The title of the ask for any particular
MsgBox window icon, so none is shown

COMP1022Q VBA Basics Page 18


Example Execution
• Excel will show the input box when the Excel file
is opened:
Press OK
The default
answer is
‘David’

You might also like