Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

Basic

VBA
Objectives
• To be able to activate the Developer Tab

• To understand the basic concept of a Macro.

• To be able to record a macro

• To be able to create a button and assign a macro to


it

• View the VBA codes as Excel records a Macro.

• To be able to hard code a macro


Macro
• Macros are little programs that run within Excel and help automate common

repetitive tasks.

• It is a recording of a series of tasks that a user does. When used right, macros
can

save you hours by automating simple, repetitive tasks.

WARNING: Actions done by a recorded macro cannot be undone. It is advisable to

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͟

1.4 Click Developer


Ribbon
Step 2: Record a Macro
Situation: We want to record a macro that will copy paste a table (see given) to a
new sheet. Given:

2.1 Click on Record Macro on the developer


tab

2.2 Input macro name ͚copy_paste͛ and click ͚ok͛


Step 2: Record a
Macro
2.3: Highlight cells: A1:A11 then copy (ctrl
+c)

4. : Click the New Sheet button


5. : Click on Cell A1 of new sheet then paste (ctrl +
V)

2.6: Click Stop


Recording
Step 3: Activating a Macro
Situation: We want to activate the macro that we just recorded. The ͚copy_paste͛
macro 3.1: Go back to Sheet 1 and click on Macros in the Developer Tab

3.2: Select ͚copy_paste͛ macro then click ͚RuŶ͛

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

4.2: Click on the ͚ButtoŶ͛ icon (See highlighted icon)

4.3: Draw the button on the


spreadsheet
Step 4: Assign a Macro to a Button
4.4: On the window that will pop-up, select ͚copy_paste͛ macro and press ͚ok͛

4.5: Click on the button to edit the caption. Put ͞ĐopLJ


paste͟

4.6: Click on the button and see


result.
Step 5: See the code of a recorded
Macro
Situation: We want to see the code of a recorded Macro
5.1: Go back to Sheet 1 and click on Macros in the Developer
Tab

5.2: Select ͚copy_paste͛ macro then click ͚Edit͛

5.3: Look at the


code
Step 6: Saving a macro-enabled
Situation: We want to save a workbook thatworkbook.
we created macros
with. 6.1: Go back to File and click ͚Saǀe͛ or ͚Saǀe As͛
• 2: Find a destination for your saved file
• 3: Change the File Name: to whatever name you like
• 4: Change the Save as type: to ͚ExĐel Macro-enabled- Workďook͛
7. See the code of a Macro while
Recording
Refer to this Given (type this to an empty sheet):

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

7.7: Click on Cell A1 of new sheet then paste (ctrl +


V)

7.8: Click Stop


Recording
Example 2
Objectives
•To create a table for candy flavors
•To understand the importance of ͞Use Relative RefereŶĐes͟
•Assign a macro a button.

Step 1: Create the Input Sheet and the Data Sheet.


•Add 2 sheets and rename them to ͞InputSheet͟ and ͞DataSheet͟

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 4: Go back to the


InputSheet Step 5: Type ͞MiŶt͟ in
cell ͚B2͛

Step 6: Go to the Developer Tab and click ͞ReĐord MaĐro͟


Example 2
Step 7: Name the new macro ͞CopyInput͟ and click ͚Ok͛

Step 8: Click on ͞InputSheet͟ on the Spreadsheets tab

Step 9: Click on cell ͚B2͛ and press ͚Ctrl + C͛ to copy


Example 2
Step 10: Go to Data Sheet spreadsheet by clicking ͞DataSheet͟ on the spreadsheet tab

Step 11: Click cell ͚A1͛

Step 12: Press ͚Ctrl + down arroǁ͛


•(you should see the selector move to the last cell downward that has an entry)

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 15: Press ͚Ctrl + V͛ to paste

Step 16: Go to Developer Tab and click ͚Use Relative RefereŶĐes͛ in order to turn it
off

Step 17: Go to the Developer Tab and click ͚Stop ReĐordiŶg͛


Example 2
Step 18: Go back to ͞InputSheet͟ spreadsheet and change the value of cell ͚B2͛ to ͚Apple͛

Step 19: Go to Developer tab and click on ͚MaĐros͛

Step 20: Choose ͚CopyInput͛ Macro and press ͚RuŶ͛

Step 21: View


Result
Example 2
Step 22: Go back to ͞InputSheet͟ and Go to Developer Tab. On the Controls Section click on ͚IŶsert͛

Step 23: Click on the ͚ButtoŶ͛ icon as seen on the picture

Step 24: Draw on the box on the ͞InputSheet͟


Example 2
Step 25: The Assign Macro window will open. Choose ͚CopyInput͛ and click ͚Ok͛

Step 26: Change the name of the button to ͚CopLJ IŶput͛

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:

2. Use 2 sheets: an ͞InputSheet͟ and a ͞DataSheet͟


3. Create the ͚CopLJ IŶput͛ button
2 ways of creating Macro
• Recording Macro
- Can be accessed through the developer ribbon in Excel (as discussed in lecture 1)
- It is useful in trying to find out codes for certain tasks that you do not know how to code.
Just simply record the macro and view the code.

• Manual Coding (Visual Basic)


- The process of creating a program through creating a series of codes to best fit the
interest of the user.
- CaŶ ďe aĐĐessed through the deǀeloper riďďoŶ iŶ EdžĐel ͚Visual BasiĐ͛

- 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

A Module is where macros could be typed


in. You could rename your Module
to become more organized, but it
really depends on the user. Some like to
leave it the way it is. But just in case you
want to change the name, it can be
found in the Properties Window.
Step 2: Your First Code
1. On the Module Window. Type to following:
Sub Hello_Sir()
Call MsgBox("Hello Mr. Polancos", vbOKOnly) End Sub

Sub – indicates the start of a code

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͟

End Sub – indicates the end of the code


Step 2: Run Your Code
1. Click anywhere in between the Sub and End Sub then Press F5 or Click the play
button.
The Window below should show:

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

You might also like