Professional Documents
Culture Documents
Introduction
What is the VBA?
VBA stands for Visual Basic for Application.
VBA is a programming language, also a macro language.
VBA cannot create standalone applications and it exists within a host application such
as Excel.
Preparation
Getting Excel to display Developer tab
1.
Right click any part of the Ribbon, choose Customize the Ribbon.
2.
In the Customize Ribbon tab of the Excel Options dialog box, locate Developer
in the second column.
3.
4.
Click OK.
2.
Enter the current date and time into the cell below.
3.
4.
Select a cell.
2.
3.
4.
Click in the Shortcut Key box and enter Shift + N (for an uppercase N) as the
shortcut key.
5.
6.
Click OK.
The Record Macro dialog box closes, and Excels macro recorder is turned on. From this
point, Excel monitors everything you do and converts it to VBA code.
2.
Move the cell pointer to the cell below and enter this formula: =NOW()
3.
Select the formula cell and press Ctrl + C to copy that cell to the Clipboard.
4.
5.
With the date cell selected, press Shift + up arrow to select that cell and the one
above it (which contains your name).
6.
Use the controls in the Home Font group to change the formatting to Bold and
make the font size 16 point.
7.
Open Module 1.
2.
3.
Saving Workbooks
If you store one or more macros in a workbook, the file must be saved as a
macro-enabled file type (*.XSLM)
2.
The VBE is a separate application where you write and edit your VBA macros.
2.
Excel takes care of opening the VBE when you need it.
3.
There is only one VBE window, and it works with all open Excel windows.
Menu bar.
2.
Toolbar.
3.
Project window: displays a tree diagram that shows every workbook currently open
in Excel.
4.
Code window: A Code window is where you put your VBA code. Every object in a
project has an associated Code window.
5.
Immediate window
2.
2.
2.
Inheritance describes the ability to create new classes based on an existing class.
3.
Polymorphism means that you can have multiple classes that can be used
interchangeably, even though each class implements the same properties or
methods in different ways.
Objects in VBA
Application
Addin
Window
Workbook
Worksheet
Range
WorksheetFunction
Collections in VBA
Wrapping Your Mind around Collections:
1.
2.
3.
Collections in VBA
Wrapping Your Mind around Collections:
1.
2.
3.
Referring to Objects
Referring to Objects is important:
1.
2.
If you want to work with the Application object, its easy: You start by typing
Application.
2.
You get to these objects by moving down the hierarchy and connecting each object on
your way with the dot (.) operator.
Examples:
To get to the Workbook object named Book1.xlsx, start with the Application object and navigate
down to the Workbooks collection object: Application.Workbooks(Book1.xlsx)
To navigate farther to a specific worksheet, add a dot operator and access the Worksheets
collection object: Application.Workbooks(Book1.xlsx).Worksheets(1)
You really want to get the value from cell A1 on the first Worksheet of the Workbook named
Book1.xlsx: Application.Workbooks(Book1.xlsx).Worksheets(1).Range(A1).Value
Sub: A Sub procedure is a group of VBA statements that performs an action (or actions) with Excel.
You need to work with a specific object in a collection (such as a particular worksheet in a workbook).
Sub ShowMessage()
MsgBox Welcome to VBA class!
End Sub
Function: A Function procedure is a group of VBA statements that performs a calculation and
returns a single value (or an array).
Function CubeRoot(number)
CubeRoot = number ^ (1 / 3)
End Function
We can talk the procedure with return value is Function and the procedure without return value is Sub.
2.
3.
Using the Ctrl + key (or Ctrl + Shift + key) shortcut assigned to the Sub procedure.
4.
Clicking a button or a shape on a worksheet. The button or shape must have a Sub
procedure assigned to it (Right-click the shape Assign Macro).
5.
From another Sub procedure that you write (Call <name of Sub procedure>).
6.
From the Immediate window in the VBE. Just type the name of the Sub procedure
and press Enter.
Sub ShowCubeRoot()
Num = InputBox("Enter a positive number")
MsgBox Num ^ (1 / 3) & " is the cube root."
End Sub
2.
Function CubeRoot(number)
CubeRoot = number ^ (1/3)
End Function
Sub CallerSub()
Ans = CubeRoot(125)
MsgBox Ans
End Sub
Macro vs Video
Preparing to record
Ultimately, the success of a recorded macro depends on five factors:
1.
2.
3.
4.
5.
Relative or Absolute
Absolute:
1.
Make sure that the Developer Code Use Relative References button is not
highlighted and then choose Developer Code Record Macro.
2.
3.
4.
5.
6.
7.
8.
9.
Relative or Absolute
Relative:
1.
2.
3.
4.
5.
Click the Use Relative References button to change the recording mode to relative.
6.
7.
8.
9.
Q&A
Thank you