Intro VBA
Intro VBA
An Introduction to VBA
Yongjun Chen
Current
EXCEL
Status
VBA
Waiting
101 Click Me to Start …
An Introduction to VBA
- Agenda
What is VBA
What is Object
How to Record Macro
VBA Editor Window
Example 1
VBA Fundamental
Create User Defined Functions (Example 2)
Work with Workbook & Worksheet
I/O with External Data Files (Example 3)
Capture Built-in Event (Example 4)
An Introduction to VBA
- What is VBA
Database Operation ….
An Introduction to VBA
- VBA- Object Based Programming Language
C++, Java, etc. are OOP (Object Oriented Programming)
Language
What is Object?
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Objects
Such as Worksheet, Workbook, Range, Cell, Chart,
Name, etc.
Worksheets(1) is an Object Referring to the First Sheet
Range("A1:B15") is an Object Referring to a Range
Cells(1,1) or Range(“A1”) is an Object Referring to
Range “A1”
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Properties
Properties are the Physical Characteristics of Objects
and Can be Measured or Quantified.
Properties for Collections
- Worksheets.Count (Read Only)
- Worksheets.Visible = True (Read and Write)
Properties for Object
- Range("A1:B15").Rows.Count (Read Only)
- Range("A1:B15").Font.Bold = True (Read and Write)
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Methods
Methods are the Actions that Can be Performed by
Objects or on Objects
Methods for Collections
- Worksheets.Add
- Worksheets.Delete
Methods for Objects
- Range("A1:B15").ClearContents
- ActiveCell.Copy
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Events
Objects Can Respond to Events, Such as Mouse Click,
Double Click on a Cell, Active a Worksheet,
Open/Close/Save a Workbook, etc.
Worksheet Events –
Such as Activate, Deactivate, Change, Selection
Change, Before Double Click, etc.
Workbook Events-
Such as Activate, Deactivate, Open, Before Close,
Before Saving, Before Print, New Sheet.
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Referring To
Use brackets () to refer to member object
Worksheets(“mooring”)
Use dot . to refer to child object or object’s
properties and methods
Worksheets(“mooring”).Range(“A1:B3”).Font.Bold
An Introduction to VBA
- VBA Object Based Programming Language
Concept Collections
(worksheets)
Summary
Objects
(worksheet)
Properties Method
name Add
Event
Activate
An Introduction to VBA
- VBA Object Based Programming Language
ActiveWorkbook.ActiveSheet.Cells(1, 1).ClearContents
Quiz 1
Which of the following is not a valid Excel object
i) ActiveWorkbook
ii) ActiveSheet
iii) ActiveCell
iv) ActiveRange
A) None B) i C) iii D) iv
An Introduction to VBA
- First Step to VBA : Macros
Record Macro
Similar to audio/video recorder
Record all the steps you conduct and write them
in VBA code
Sample
Record Macro
See Demo
Understand Macro
Modify Macro
Assign Macro to Button Click Event
An Introduction to VBA
- Second Step to VBA : Edit Macro
Quiz 2
For the following four actions
i) Type formula in another workbook/file
ii) Change macro security level
iii) Print another worksheet in the same workbook
iv) Open a Word file
Fundamental of VBA
Fundamental of VBA
Fundamental of VBA
Function
Public , Private, Return Value
Parameter
Subroutine
Public, Private, No Return Value
Parameter
An Introduction to VBA
- Third Step to VBA : Write VBA Code
Understand Code
Worksheets("sheet1").Activate
Range("A1:A10").ClearContents 1) Make sheet1 active
Quiz 3
Function myTest(a as integer, b as integer) as integer
myTest = a + b
myTest = myTest + 1
Range("D1").Value = myTest
MsgBox "All done”
End function
Which statement will cause problem for myTest
A) 1st one B) 2nd one C) 3rd one D) 4th one
An Introduction to VBA
- Third Step to VBA : Write VBA Code
Questions / Comments
Where to get help:
Use F1 key in VBA editor (like type keyword msgbox to search help)
Microsoft Users Excel Programming Discussion
www.microsoft.com/office/community/en-
us/default.mspx?dg=microsoft.public.excel.programming&lang=en&cr=US
http://www.excelforum.com/