Intro VBA
Intro VBA
An Introduction to VBA
Yongjun Chen
Current
EXCEL
Status
VBA
101
Waiting 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
An abbreviation for Visual Basic for Applications
Official name is "Visual Basic, Applications Edition."
VBA is Microsoft's common application programming
(macro) language for Word, Excel, Access, etc.
Also being implemented in other Microsoft applications
such as Visio and is at least partially implemented in
some other applications such as AutoCAD...
VBA and VB have a lot in common, but they are
different. VB is a programming language that lets you
create standalone executable programs.
An Introduction to VBA
- What Can You Do With VBA
Write Macros to Automate Labor-Intensive and Repetitive
Tasks
What is Object?
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Containers or Collections
A Group of Similar Objects Share Common Properties,
Methods and Events
Such as Workbooks, Worksheets, etc.
Worksheets is a collection of all the Worksheet objects
in the specified or active workbook.
Worksheets(1) refers to the 1st worksheet of current
active workbook.
Worksheets (“mooring”) refers to the worksheet named
“mooring”.
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
Objects
(worksheet)
Properties Method
name Add
Event
Activate
An Introduction to VBA
- VBA Object Based Programming Language
Understand Object Concepts
Workbooks("book1.xls").Worksheets("sheet1").Range("A1").Font.Bold = True
ActiveWorkbook.ActiveSheet.Cells(1, 1).ClearContents
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
Record Macro
Understand Macro
Modify Macro
Assign Macro to Button Click Event
See Demo
An Introduction to VBA
- Second Step to VBA : Edit Macro
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
Create User-Defined Functions See Demo
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/