Introduction To Excel VBA: Hkcareers Beginners Manual
Introduction To Excel VBA: Hkcareers Beginners Manual
TABLE OF CONTENTS
1. Introduction
2. VBA Overview
3. Excel Macros
4. Excel Terms
5. VBA Macro Comments
6. VBA Message Box
7. VBA Input Box
8. VBA- Variables
9. VBA- Constants
10.VBA- Operators
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
With Excel VBA you can automate tasks in Excel by writing so called macros. In this chapter,
we will look at the step-wise procedure to create a simple macro.
To open a VBA window, press "ALT+F11" in your Excel window. The screenshots below
should be able to guide you through the steps.
Step 2: Drill down on ‘Customize the Ribbon’ tab → check 'Developer' tab → Click 'OK'.
(you can locate the Developer tab below the View tab)
*Source: Google
Step 3: You will now see the 'Developer' ribbon in the menu bar
*Source: Google
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
*Source: Google
Step 4: Click the 'Visual Basic' button to open the VBA Editor.
*Source: Google
Step 5: Start scripting by adding a button. To assign a macro (one or more code lines) to the
command button, execute the following steps.
*Source: Google
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
*Source: Google
Step 7: Edit the Name and Caption as shown in the following screenshot.
*Source: Google
Step 8: Now double-click the button and the sub-procedure outline will be displayed as
shown in the following screenshot.
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
*Source: Google
Step 10: Click the button to execute the sub-procedure. The output of the sub-procedure is
shown in the following screenshot.
*Source: Google
In this section, we will discuss the commonly used excel VBA terminologies which we will
encounter in the following sections. Hence it is important to understand them at this stage.
Line of Code
This a VBA instruction as we have seen above. Generally speaking, it performs one task.
Sub
A sub is made up of one or more lines of code. We create a sub so that VBA will process the
instructions we give it. To do this we get VBA to Run the sub. When we “Run” the sub,
VBA goes through all the lines of code and carries out the appropriate actions. A macro and a
sub are essentially the same thing.
Modules
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching
HKCareers Free Resource
Modules is the area where the code is written. A module contains subs which in turn contain
lines of code. There is no reasonable limit to the number of modules in a workbook or the
number of subs in a module that can be created.
*Source: Google
*Source: Google
Within the modules, we can write VBA code and the code is written within a Procedure. A
Procedure is a unit of code enclosed either between the Sub and End Sub statement or
between the Function and End Function statements.
Procedure
Procedures are a group of statements executed as a whole, which instructs Excel how to
perform a specific task. The task performed can be a very simple or a very complicated task.
However, it is a good practice to break down complicated procedures into smaller ones.
We help university students to get into Investment Banks, Banks, Property/Conglomerate and Big4s.
Prepared by HKCareers | IG: hkcareers | Facebook: hkcareers | Website: hkcareers.hk/coaching