Introduction To Excel VBA Macros
Introduction To Excel VBA Macros
Introduction To Excel VBA Macros
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 Introduction to VBA
Database Operation .
An Introduction to VBA
What is Object?
An Introduction to VBA
An Introduction to VBA
Concepts Objects
An Introduction to VBA
Concepts Properties
An Introduction to VBA
Concepts Methods
An Introduction to VBA
Concepts Events
An Introduction to VBA
Concepts Referring To
Worksheets(mooring)
Worksheets(mooring).Range(A1:B3).Font.Bold
An Introduction to VBA
Concept
Summary
An Introduction to VBA
Workbooks("book1.xls").Worksheets("sheet1").Range("A1").Font.Bold = True
ActiveWorkbook.ActiveSheet.Cells(1, 1).ClearContents
Private Sub Workbook_Open()
MsgBox "Thank you for choosing VBA"
End Sub
An Introduction to VBA
Quiz 1
i) ActiveWorkbook
ii) ActiveSheet
iii) ActiveCell
iv) ActiveRange
A) None
B) i
C) iii D) iv
An Introduction to VBA
Record Macro
An Introduction to VBA
An Introduction to VBA
An Introduction to VBA
An Introduction to VBA
Use Tools/Options to Enable Auto Syntax Check, Auto List Members, etc.
Use Tools/Properties to Protect Your Code with Password You Must Remember the Password
Insert Module, Procedure, Form
An Introduction to VBA
Sample
Record Macro
Understand Macro
Modify Macro
Assign Macro to Button Click Event
See Demo
An Introduction to VBA
Property window
Code window
Show properties of
active object and
let user to modify
the properties
An Introduction to VBA
Quiz 2
A) ii only
B) iv only
C) ii & iv
D) iii & iv
An Introduction to VBA
Fundamental of VBA
Variables: can be define explicit or implicit use dim statement
Constant use const pi =3.1415926
Function: pair of function end function
Subroutine: pair of sub end sub
Comment use single quotation
Continuation use underline _
Use with end with for object
Assign object use set and assign data variable use =
An Introduction to VBA
Fundamental of VBA
Decision Making use If elseifelseend if
Multiple Selection/Decision Making
Select Case Var Case A Case BCase Else End Select
Loop use
Do While Loop
Do Loop Until
For Next
For Each Next
Array dim Data(10) as integer , Val(2 to 8) as object
An Introduction to VBA
Fundamental of VBA
Function
Public , Private, Return Value
Parameter
Subroutine
Public, Private, No Return Value
Parameter
An Introduction to VBA
Understand Code
Worksheets("sheet1").Activate
Range("A1:A10").ClearContents
2)
For i = 1 To 10
Range("A" & i) = i ^ 2
3)
Next
Range("A1:A10").Font.Bold = True
For Each cl In Range("A1:A10")
If cl.Value < 25 Or cl.Value > 75 Then 4)
cl.Font.Italic = True
Else
cl.Font.Underline = True
5)
End If
Next
Msgbox All done
An Introduction to VBA
Quiz 3
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
See Demo
Arguments
An Introduction to VBA
Work
with
Workbook
Use With
Statement
(faster)
An Introduction to VBA
An Introduction to VBA
Work
with
Worksheet
An Introduction to VBA
An Introduction to VBA
An Introduction to VBA
See Demo
Sub Input00()
Dim tmpstr As String
Open ThisWorkbook.Path & "\test.dat" For Input As #1
Line Input #1, tmpstr
Line Input #1, tmpstr
Range(A1").Value = tmpstr
Close #1
End Sub
An Introduction to VBA
See Demo
An Introduction to VBA
An Introduction to VBA
An Introduction to VBA
An Introduction to VBA
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/