10 - SYSINFO - OOAA - Excel Basic VBA
10 - SYSINFO - OOAA - Excel Basic VBA
VBA
Objectives
• To be able to activate the Developer Tab
repetitive tasks.
• It is a recording of a series of tasks that a user does. When used right, macros
can
save your file before activating a Macro to prevent data loss on spreadsheets.
Creating your
first Macro
(via Record
Macro)
Exam
ple 1
Step 1: Activate Developer Toolbar
1. Click on File, and select Options
2. On the options menu window, click on the ͞Custoŵize RiďďoŶ͟ taď͟
Step 1: Activate
Developer Toolbar
1.3 Click the tick box for ͞Deǀeloper͟ and click ͞OK͟
3.3: Look at
result
Step 4: Assign a Macro to a
Button
Situation: We want to be able to activate the ͚copy_paste͛ macro with just clicking one
button. 4.1: Go back to Sheet 1 and click on Insert in the Developer Tab
7.1 Press Alt + F11 or click ͚Visual BasiĐ͛ on the developer tab
7.2 The visual basic screen will show up. Put it side by side with the Excel
Window.
7. See the code of a Macro while
3.
4.
Recording
Click on Record Macro on the developer tab
Input macro name ͚ĐopLJ_paste_2͛ and click ͚ok͛
7.5 Highlight cells: A1:A11 then copy (ctrl +c). Notice on the right side that VBA will display a code for every
move the user makes.
7. See the code of a Macro while
Recording
7.6: Click the New Sheet button
Step 2: Create where a new entry should be placed in the Input Sheet. Copy the format
below:
Example 2
Step 3: Create the table for the candy flavors. Copy the Format below
Step 13: Go to the Developer Tab and Press ͚Use Relative RefereŶĐes͛ to turn it
on
•This action turns the currently selected cell as a reference point.
Example 2
Step 14: Press ͚doǁŶ arroǁ͛
•This action makes sure that the selector will go down one cell with respect to the reference point cell
Step 16: Go to Developer Tab and click ͚Use Relative RefereŶĐes͛ in order to turn it
off
Step 27: Change the value of cell ͚B2͛ to ͚CoĐoŶut ͛ and press ͚CopLJ IŶput͛ button.
Step 28: View Result:
Exercise 1
Objective:
1. To be able to Mimic the given
table:
- Coding through Visual Basic is more flexible since the user is the one creating the codes.
In
this ŵaŶŶer, the prograŵ ĐaŶ ďe ŵore persoŶalized to he user͛s satisfaĐtioŶ.
Visual Basic Editor
Step 1: Accessing Visual Basic
Editor
•Click the ͚Visual BasiĐ͛ icon
from the developer tab in Excel
•Press Alt+F11 in Excel.
The window on the right will show up.
Toolbar: shows the tools you will
need to create your program
Project Explorer: shows the current
workbooks, worksheets, and modules
that you have.
Properties Window: shows the
properties of the selected entity in the
project explorer
Standard Toolbar
ICONS (from left to right)
1.View Microsoft Excel – brings you back to your 11. Break – breaks the run of the code you have
Excel workbook created
2.Insert User Form –inserts a User Form into the 12. Reset – stops the whole run of the code and
project (User Forms will be discussed later on) 13. Design Mode – Click only geometrical
•Save – saves current progress functions (alignment, size etc. in a user form.
Useful in creating user forms)
1.Cut – cut a selected text
14. Project Explorer – shows the project explorer
•Copy – copy a selected text
15. Properties Window – shows the properties
1.Paste – paste the cut/copied text window
2.Find – look for a text in a body of codes 16. Object Browser – shows the object browser
3.Undo 17. Tool Box – shows the toolbox
4.Redo 18. Help
5.Run Macro (F5) – runs the code you have
created
Creating your
first Macro
(via Coding)
Step 1: Inserting a Module
1. On the taskbar, click on Insert,
and select Module
A ͚Module1͛ should appear on the Project
Explorer and a ͚Book 1 – Module1͛
window should appear
Name() – indicates the name of the code , it could be anything that the user wants. BUT it must
contain any space. So rather than typing ͞Hello Sir,͟ we type ͞Hello_Sir͟ or ͞HelloSir͟
Congratulations! That was your first code! This exercise was only to familiarize you
with VBA and make you realize that there are things that Manual Coding can do
that Recording a Macro ĐaŶt. ͛ Example of these are Message Boxes and User
Forms.
Exercise 2
Create the codes to make the following Message
Boxes appear. Please fill the information
inside the ͞< >͟
•Create a macro named ͞Hello_DLSU͟ and
create the code to make the message box on the
right appear:
•Create a macro named ͞My_Favorite_Artist͟ and
create the code to make the message box on
the right appear:
•Create a macro named
͞TheBestThingAboutMe͟ aŶd Đreate the code to
make the message box on the right appear:
Exercise 2 (answers)
Create the codes to make the following Message
Boxes appear. Please fill the information
inside the ͞< >͟
•Create a macro named ͞Hello_DLSU͟ and Sub Hello_Dlsu()
create the code to make the message box on the Call MsgBox("Hello DLSU, my name is <insert your name>. I am <insert
your age> years old", vbOKOnly)
right appear: End Sub
•Create a macro named ͞My_Favorite_Artist͟ and Sub My_Favorite_Artist()
create the code to make the message box on Call MsgBox("My Favorite Artist is <insert name of favorite
artist> because <he/she> is <insert adjective>", vbOKOnly)
the right appear: End Sub
•Create a macro named Sub TheBestThingAboutMe()
͞TheBestThingAboutMe͟ aŶd Đreate the code to Call MsgBox("The best thing about me is <insert the best thing about
you>", vbOKOnly)
make the message box on the right appear: End Sub