0% found this document useful (0 votes)
9 views

Chapter 5. VBA (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Chapter 5. VBA (1)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 5.

VBA

What is Excel VBA?


Excel VBA, short for Visual Basic for Applications, is a programming language that
empowers users to automate tasks and create personalized solutions within Microsoft
Excel.
Visual Basic for Application is a human-readable and editable programming code
that gets generated when yourecord a macro.
Macros are what most people who write VBA code use.

Why Create Macros?


Macros are programs that make changes to a spreadsheet automatically
Change a font size or color
Move the cursor to another cell
Change the contents of a cell
Almost anything you can do from the menus can be automated
Create custom functions
Simple process – record a macro

VBA Editor Interface

Open the VBA interface by using the ALT + F11 keyboard shortcut,
or go to the Developer tab and click on Visual Basic.

VBA Objects

Usually referred to by a name


Cell “B6” is named Range(“B6”)
Column(“B:B”) refers to the entire Column
Nicknames for current cells, sheets
ActiveCell
ActiveSheet
Objects of same type form a collection
Worksheets(…) is the collection
Worksheets(“Sheet1”) refers to one object

Object Hierarchy ( mô hình cấp bật trong excel )


Phải truy cập workbook xong mới tới sheet
Assignment Statements (chuyển từ Cells này qua cell khác)

Assignment statements change an object


This statement puts the number 15 into cell A3:
Range(“A3”).value = 15
This statement determines the value in cell A3, and puts it in cell C4:
Range(“C4”).value = Range(“A3”).value
• This one adds the value in A3 to what’s in C4:
Range(“C4”).value = Range(“C4”).value + _ Range(“A3”).value

Objects and classes in Excel

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

InputBox (hộp thư nhập) MsgBox Function (hộp thư


thông báo)

Visual Basic for Applications

1. Structure of Procedure (module)

Sub name_of_program (parameters,…)

<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

2/ Do until loop vs do loop until


do loop until do loop until

3/ If them, else if and else


If them else if else
  

You might also like