Chapter 5. VBA (1)
Chapter 5. VBA (1)
VBA
Open the VBA interface by using the ALT + F11 keyboard shortcut,
or go to the Developer tab and click on Visual Basic.
VBA Objects
Subroutines
Begin With Sub MyName()
End withEnd Sub
Location
Modules
Worksheets
Workbook
UserForm
Relative Addresses
• Defines one cell relative to another
• Offset(Row, Column)
Rows: positive to right
Columns: positive means move down
Range(“A3”).Offset(2,4) refers to cell E5
Activecell.Offset(1,0).Select moves cursor down one cell.
Variables
Short-term storage for information
Held in RAM, not stored to disk
Disappears when program stops running
Needs a name
Must be unique
Can’t be the same as objects, other variables
Can’t be a key word to VBA
I often use vName
Dim vName
<statements>
…..
End sub
2. Reading data from active sheet
3.Naming ranges is useful for creating programs in VBA
Sheets(“Sheet name”).Range(“Start”)
4. Dialog Boxes allow dialog with end-user of an application
Sub computer (optional Year)
Year =InputBox(“Input year of craetion of the
first PC”)
If Year = 1976 Then msg=“True!”
Else msg=“False!”
MsgBox msg
End Sub
References to objects đọc slide
1. For next loop vs For each loop
For next loop For each loop