VBA Lesson1
VBA Lesson1
VISUAL BASIC
PROGRAMMING from
EXCEL
Getting Started
Lecture 1
Department: Process Automation Engineering
Lecturer: Aygul Musayeva
2
What is Visual Basic?
Visual Basic (VB) is a
programming language that
runs with Microsoft Office
products.
VB is used for improving the
functionality of Excel
spreadsheets.
VB is used to develop a couple
of tools for “standard design
tasks”, such as pipe, pump and
heat exchanger sizing.
13/05/2024
3
What is Visual Basic?
Visual Basic is a more recent variant of the old
BASIC that was developed in 1960s.
13/05/2024
6
Setting up a Function in VBA
Example: Create a VISUAL BASIC function within Excel that
takes an input parameter and doubles it.
13/05/2024
7
13/05/2024
8
13/05/2024
9
13/05/2024
10
13/05/2024
16
13/05/2024
17
Details
It makes sense to use function names which
makes it more intuitive to use them. In the
present case “double” seems attractive. But, if
you try to type “Public Function double(x)”, you
will get an error message. The reason is, that
“Double” is defined and used already, as data
type. However, “multiply_by_2” will work.
13/05/2024
20
13/05/2024
21
Variable Assignment
The first variable assignment was done
essentially automatic in the function declaration.
However, values can also be assigned to variables
directly:
y=5
From now on y can be used and its value will be 5.
Variable Assignment
Another way to assign values is through a
mathematical operation, such as
z=x*y
or
test1 = 2 * x
13/05/2024
23
Exercises
1. In many cases our VB functions will require more than
one argument. This is easily achieved: In the function
declaration replace “test1(x)” by “test1(x, y)”. Now, when
you call the function from the spreadsheet, you need to
provide two arguments, for example “=test1(A1,A2)”. Test
functions with more than one argument.
Exercises
4. Design a function that calculates the ideal gas pressure in
MPa from the temperature in °C, the volume in m3 and the
number of moles in kmol.
13/05/2024