0% found this document useful (0 votes)
25 views25 pages

VBA Lesson1

This document discusses Visual Basic programming from Excel. It introduces VB and its functions, how to set up a basic doubling function in VB, and how to call and modify that function from Excel. The document provides examples of variable assignment, arithmetic operators, and exercises for the reader to practice VB functions.

Uploaded by

Aytac Vəliyeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views25 pages

VBA Lesson1

This document discusses Visual Basic programming from Excel. It introduces VB and its functions, how to set up a basic doubling function in VB, and how to call and modify that function from Excel. The document provides examples of variable assignment, arithmetic operators, and exercises for the reader to practice VB functions.

Uploaded by

Aytac Vəliyeva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Baku Hi ghe r Oi l School

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.

It is a high-level programming language along with


C, C++, FORTRAN, JAVA and many others. “High-
level” means that you don’t have to worry about how
the computer does things.

Visual Basic is an “object oriented” language, which


means that you can define and then use objects.
Objects are things such as buttons, scroll-down
menus, switches, etc.
13/05/2024
4
Functions in VBA
VB can be used to program new functions for Excel,
which you can call from a spreadsheet as you call all
predefined functions. This allows you to do things that
you can not do with standard Excel functions. It also
allows you to produce cleaner spreadsheets as you can
move thinks like documentation and unit conversion into
the VB function.

Excel and VB do not convert units for you, so you


have to be disciplined in using the right units and
program their conversion.
13/05/2024
5
Functions in VBA
VISUAL BASIC is a programming environment
that is accessible from Microsoft Office
products.
Within Excel, VISUAL BASIC exists in two
forms: as SUBPROGRAMs and as
FUNCTIONs.
Functions behave very similarly to Excel’s built
in functions.

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.

1. In Excel, select VISUAL BASIC on the Developer tab or


press Alt+F11. This brings up the VB editor.

13/05/2024
7

Setting up a Function in VBA


1. If the Developer tab is missing, click FileOptions
In the new window chose Customize Ribbon and then
tick the Developer check box.

13/05/2024
8

Setting up a Function in VBA


Once you have checked the Developer check box, this
setting will be retained when you use Excel in future.
The editor window has two parts: The left part shows the
workbook’s contents and any MODULES. Modules are
collections of VB routines (subprograms and functions) in
the workbook.

13/05/2024
9

Setting up a Function in VBA


2. Create a new module.
Use the INSERT  MODULE menu command.
The new window, which opens, is where
program code will be typed.
3. Start a new subprogram using the
INSERT  PROCEDURE pull down menu; a
window will pop up:

13/05/2024
10

Setting up a Function in VBA

4. Select “Function” in the Type part and give the


function a name. Here it is “test1”. Function names can
contain letters and numbers, but they cannot use spaces. The first
character must be a letter.
5. Leave the Scope setting at Public for now.
13/05/2024
11

Setting up a Function in VBA

The first lines of code put in by the system


“Public Function…” is the function declaration
and also demarcates the beginning of the
function code. The “End Function” defines the
end of the function. All commands typed in
between
13/05/2024 these two lines are the function itself.
12

Setting up a Function in VBA


6. Now use the Module window like a word processor
and type in commands.

As you finish a line of code and press <Enter>, the


system will change the colors of some words (syntax
highlighting) and start command words with a capital
letter.
13/05/2024
13

Setting up a Function in VBA


Colors which you will see are:
• GREEN for all comment lines. These lines are
used by the programmer to describe what the
program is doing.
• BLACK used for non-command lines which are
usually bits of maths.
• BLUE Commands which the system knows
about.
• RED Something is incorrect.
Practice the use of comments!
13/05/2024
14

Saving the Spreadsheet


The first time you save the program, you must
select “Save As Excel Macro-Enabled
Workbook” from the File menu.

Subsequently, you can use the simple SAVE


button or the Ctrl+s shortcut.
13/05/2024
15

Using the Function


You can now use this function as a normal build-in
function. Go to the spreadsheet write a number into
some cell and in another cell start to write the
command to call the new function: “= test1”. At this
point Excel already recognizes the new function.

13/05/2024
16

Using the Function


Now complete the function call: “= test1(A1)”

and press <Enter>.

13/05/2024
17

Using the Function

The function is executed and the result, 6 is written into the


cell. The function is still show in the function line (red
arrow).
As with build-in functions Excel will reevaluate the function
as soon as its input changes.
13/05/2024
18
Details
The function name, “test1” is used to call the function from
the spreadsheet.
The value of the function evaluation must be assigned
to the variable with the same name, “test1” inside the
function code. We have done this in the line “test1 = 2 * x”.
If you fail to do this, Excel will return the default value of
test1, which should be Zero.
 The function requires an input parameter. This is
specified in the function declaration line: “Public Function
test1(x)” by the x in the parenthesis. It states that exactly
one input parameter is required and assigns its value to the
variable x within the function code.
13/05/2024
19

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

Modifying the Function


If you modify the function in the module editor, it will not
automatically be updated on the spreadsheet. There
are various ways to force a re-evaluation:

1. Retype or modify one of the input parameters and


press <Enter>. In the present example that would be
the value in cell A1.
2. Mark the cell with the function call, move the cursor
into the function editor line, click once and press
<Enter>.

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.

Variables should never be used without assigning


a value first. This is possible and the default value
would be used. In VB the default should be Zero.
13/05/2024
22

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

Arithmetic operators in VBA


Function Operator symbol
Addition +
Multiplication *
Division /
Subtraction -
Exponentiation ^
String concatenation &
Integer division (the result is always \
an integer)
Modulo arithmetic (returns the MOD
remainder of division operation)
13/05/2024
24

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.

2. Computer programs allow us to use operations like


y = y +3
This is not an implicit equation for y! Here the current value of
y is added to 3 and the result reassigned to y. Test
operations of this form.
13/05/2024
25

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

You might also like