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

VBA Programming

Pdf files

Uploaded by

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

VBA Programming

Pdf files

Uploaded by

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

MICROSOFT

EXCEL VBA
ENGR. FRAMCES THEA DE MESA, PECE
VISUAL BASIC ENVIRONMENT
To go to Visual Basic Environment:
1. Check if the Developer tab is activated.

if it is not activated, activate the Developer tab.


- click on the File tab
- click More Options
- click Customize Ribbon
- check the Developer checkbox
- click OK
VISUAL BASIC ENVIRONMENT

2. Click on the Visual Basic button in area Code.


VISUAL BASIC ENVIRONMENT

2. Click on the Visual Basic button in area Code.


VISUAL BASIC ENVIRONMENT
ELEMENTS OF VISUAL BASIC
ENVIRONMENT
MENU BAR
There are standard menus, like many windows of the operating system:
File, Edit, View, Tools and Help.
The Insert menu is used for organizing a place for program storage.

Menus Debug and Run are used for debugging and running the
program respectively.
ELEMENTS OF VISUAL BASIC
ENVIRONMENT
TOOL BAR

- use for common actions like saving, undo and redo actions, play, stop etc.

- use for editing texts and program codes

- use for debugging programs

- in designing the user form (GUI)


VBA Terminologies
VBA Terminologies
VBA Terminologies
VBA Terminologies
VBA Terminologies
PROGRAM DEVELOPMENT
Development of program begins with inserting a module into the
active Excel workbook.
– To insert a module, click on the line VBAProject (Book1) in the
project explorer window
PROGRAM DEVELOPMENT
Development of program begins with inserting a module into the
active Excel workbook.
– To insert a module, click on the line VBAProject (Book1) in the
project explorer window
PROGRAM DEVELOPMENT
– Insert > Module

• To open the Code window of a previously inserted module, double


click on its name in the Project Explorer Window.
• To delete a previously inserted module, right click on its name and
select Remove
PROGRAM DEVELOPMENT
• The first and last line of the program is standard.

• The name of the subroutine must satisfy the following conditions:


- the first character should be a letter
- must include only letters, figures and the underscore character
- must be less than 256 characters
• To use name consisting of several words
- you may capitalize each word
- you may use underscore as space
PROGRAM DEVELOPMENT

• pythagoras – is the name of the subroutine


• a,b and c are variable names
• Sqr – is a keyword for the function of taking the square root
• ^ - is a mathematical operator
• Visual Basic Environment is tuned in such a manner that all keywords
are highlighted in blue color, comments are highlighted in green color
and errors are in red.
PROGRAM DEVELOPMENT

• Before running the program, the Locals Window should be displayed. It


is where we would see the current value of the local variables in the
program. If it is not yet displayed, enable its display in the View Tab.
• To run the program, click on the left side of End Sub.

• Then click the “play” button on the standard toolbar.


PROGRAM DEVELOPMENT

Click Run.
PROGRAM DEVELOPMENT
An apostrophe (‘) means that information following it is a comment, i.e.,
a character set, which does not influence the program execution.

To save the program,


1) In the Excel window, File > Save As > Browse;
2) In the Save As window opened, choose a folder for saving the
workbook
3) By means of drop-down list Save as type, select the file type
Excel Macro-Enabled Workbook
4) Click on Save button.
VBA Variables
Variable is a named memory location used to hold a value that
can be changed during the script execution.

Following are the basic rules for naming a variable.


• You must use a letter as the first character.
• You can't use a space, period (.), exclamation mark (!), or the
characters @, &, $, # in the name.
• Name can't exceed 255 characters in length.
• You cannot use Visual Basic reserved keywords as variable
name.

In VBA, you need to declare the variables before using them.


VBA Variables
There are many VBA data types, which can be divided into two
main categories, namely numeric and non-numeric data types.
Numeric Data Types
VBA Variables
Non-Numeric Data Types
VBA Constants

Constant is a named memory location used to


hold a value that CANNOT be changed during the script
execution. If a user tries to change a Constant value,
the script execution ends up with an error. Constants are
declared the same way the variables are declared.

Following are the rules for naming a constant.


• You must use a letter as the first character.
• You can't use a space, period (.), exclamation mark
(!), or the characters @, &, $, # in the name.
• Name can't exceed 255 characters in length.
• You cannot use Visual Basic reserved keywords as
variable name.
VBA Constants
VBA Operators

An Operator can be defined using a simple expression - 4 + 5


is equal to 9. Here, 4 and 5 are called operands and + is
called operator. VBA supports following types of operators:

A. Arithmetic Operators
VBA Operators

B. Comparison Operators
VBA Operators

C. Logical Operators
VBA Operators

C. Concatenation Operators

Note: Concatenation Operators can be used for both numbers and


strings. The output depends on the context, if the variables hold
numeric value or string value.
MESSAGE BOX
The MsgBox function displays a message box and waits for the user
to click a button and then an action is performed based on the button
clicked by the user.

Prompt - A Required Parameter. A String that is displayed as a


message in the dialog box. The maximum length of prompt is
approximately 1024 characters. If the message extends to more than
a line, then the lines can be separated using a carriage return
character (Chr(13)) or a linefeed character (Chr(10)) between each
line.
Buttons - An Optional Parameter. A Numeric expression that specifies
the type of buttons to display, the icon style to use, the identity of the
default button, and the modality of the message box. If left blank, the
default value for buttons is 0.
Title - An Optional Parameter. A String expression displayed in the
title bar of the dialog box. If the title is left blank, the application
name is placed in the title bar.
MESSAGE BOX

The Buttons parameter can take any of the following


values:
• 0 vbOKOnly - Displays OK button only.
• 1 vbOKCancel - Displays OK and Cancel buttons.
• 2 vbAbortRetryIgnore - Displays Abort, Retry, and
Ignore buttons.
• 3 vbYesNoCancel - Displays Yes, No, and Cancel
buttons.
• 4 vbYesNo - Displays Yes and No buttons.
• 5 vbRetryCancel - Displays Retry and Cancel buttons.
• 16 vbCritical - Displays Critical Message icon.
• 32 vbQuestion - Displays Warning Query icon.
• 48 vbExclamation - Displays Warning Message icon.
• 64 vbInformation - Displays Information Message icon.
MESSAGE BOX

• 0 vbDefaultButton1 - First button is default.


• 256 vbDefaultButton2 - Second button is default.
• 512 vbDefaultButton3 - Third button is default.
• 768 vbDefaultButton4 - Fourth button is default.
• 0 vbApplicationModal Application modal - The current
application will not work until the user responds to the
message box.
• 4096 vbSystemModal System modal - All applications will not
work until the user responds to the message box.

The above values are logically divided into four groups:


The first group (0 to 5) indicates the buttons to be displayed
in the message box. The second group (16, 32, 48, 64)
describes the style of the icon to be displayed, the third group
(0, 256, 512, 768) indicates which button must be the default,
and the fourth group (0, 4096) determines the modality of
the message box.
MESSAGE BOX
INPUT BOX
The InputBox function prompts the users to enter values. After
entering the values, if the user clicks the OK button or presses ENTER
on the keyboard, the InputBox function will return the text in the text
box. If the user clicks the Cancel button, the function will return an
empty string ("").

• Prompt - A required parameter. A String that is displayed as a


message in the dialog box. The maximum length of prompt is
approximately 1024 characters. If the message extends to more than
a line, then the lines can be separated using a carriage return
character (Chr(13)) or a linefeed character (Chr(10)) between each
line.
• Title - An optional parameter. A String expression displayed in the title
bar of the dialog box. If the title is left blank, the application name is
placed in the title bar.
• Default - An optional parameter. A default text in the text box that the
user would like to be displayed.
INPUT BOX
DECISION MAKING
CONSTRUCTS
When, in a certain place of a program, it is necessary to execute those
or other operators depending on some conditions. The choice of the
operators is performed by means of one of two decision-making
constructs, If…Then or Select Case.

If condition Then statement1 (condition is a logical expression)


DECISION MAKING
CONSTRUCTS
If condition Then statement1 Else statement2

When conditions are more than 1, then the construct is:


CYCLES
To execute repeatedly an operator block, we can use one of the three
cycle operators, For…Next, While…Wend and Do…Loop.

For…Next cycle

Where counter is a variable of numerical data type, beginning and


ending are the boundaries of the counter change, growth is the step of
this change.
CYCLES
While…Wend cycle
The While.. Wend cyle used when the number of the block’s executions
is not known in advance.
Thank you!!
and
God Bless po!!!

You might also like