Upload
cedric-hodoreanu
View
220
Download
0
Embed Size (px)
Citation preview
8/10/2019 01 Introducing VBA
1/21
MS ExcelVBA Programming
Introducing VBA
8/10/2019 01 Introducing VBA
2/21
Introducing VBA Programming
Visual Basic for Applications (VBA) aprogramming language that helps program,tweak, and squeeze productivity from Excel
Embedded in Excel OOP - object-oriented programming
8/10/2019 01 Introducing VBA
3/21
What Can You Do with VBA?
Inserting a text string Automating a task you perform frequently Automating repetitive operations (loops) Creating a custom command Creating a custom toolbar button
Creating a simplified front end Developing new worksheet functions Creating complete, macro-driven applications
Creating custom add-ins for Excel
8/10/2019 01 Introducing VBA
4/21
Advantages and Disadvantages of VBA
Advantages Disadvantages
Executes the task in exactly the same way
Learning how to write programs in VBA (language syntax, objects, props,)
Much faster than you could do itmanually
Other people who need to use your VBA programs must have their owncopies of Excel
Always performs the task withouterrors
Sometimes, things go wrong
Task can be performed by someone who doesnt know anything aboutExcel
VBA is a moving target
Can do things in Excel that areotherwise impossible
8/10/2019 01 Introducing VBA
5/21
VBA in a Nutshell (1)
You perform actions in VBA by writing (orrecording) code in a VBA module
A VBA module consists of Sub procedures(macros and/or UDFs)
VBA manipulates objects Objects are arranged in a hierarchy Objects of the same type form a collection You refer to an object by specifying its position
in the object hierarchy, using a dot as aseparator
8/10/2019 01 Introducing VBA
6/21
VBA in a Nutshell (2)
If you omit specific references, Excel uses theactive objects
Objects have properties and methods You refer to a property of an object by
combining the object name with the propertyname, separated by a period
You can assign values to variables VBA includes all the constructs of modern programming languages, including arrays andlooping
8/10/2019 01 Introducing VBA
7/21
Jumping Right In
Recording macro vs Using Excel built-in function (for paste special, values only)
Live view macro recording sample
8/10/2019 01 Introducing VBA
8/21
Visual Basic Editor (VBE)
Is connected, stillseparateapplication
Used to write, editand test code
Alt + F11
8/10/2019 01 Introducing VBA
9/21
Working with the Project Explorer
Each Excel workbook andadd- in thats openis a project
Ctrl + R
8/10/2019 01 Introducing VBA
10/21
Customizing the VBA Environment
Use the option in the Editor tab to control howcertain things work in the VBE
8/10/2019 01 Introducing VBA
11/21
Excel Object Model
8/10/2019 01 Introducing VBA
12/21
Excel Application Object
Addin object
CommandBar object
Window object
Workbook object
WorksheetFunction object
8/10/2019 01 Introducing VBA
13/21
Workbook object
Chart object
Name object
VBProject object
Window object
Worksheet object
8/10/2019 01 Introducing VBA
14/21
8/10/2019 01 Introducing VBA
15/21
Collections of Objects
A collection is a group of objects of the sametype And a collection is itself an object !
Few examples of commonly used collections: Workbooks collection
Worksheets collectionCharts collectionSheets collection
collection names are all plural: Chart>Charts
8/10/2019 01 Introducing VBA
16/21
Referring to Objects (members of a collections)
Worksheets(Sheet1) by name Worksheets(1) by order
8/10/2019 01 Introducing VBA
17/21
8/10/2019 01 Introducing VBA
18/21
Object Properties and Methods
In order to manipulate objects:
Read or modify an objects properties
Specify a method of action to be used with an
object
8/10/2019 01 Introducing VBA
19/21
Object properties
Every object has properties. Properties are attributes that describe the object,
determine how it looks, how it behaves, and even whether it is visible.
Using VBA, you can do two things with an
objects properties: Examine the current setting for a property Change the propertys setting
8/10/2019 01 Introducing VBA
20/21
Object methods
A method is an action you perform with anobject
Most methods also take one or more arguments. An argument is a value that further specifies theaction to perform.
Place the arguments for a method after themethod, separated by a space. Multiplearguments are separated by a comma.
8/10/2019 01 Introducing VBA
21/21
Object events
Objects respond to various events that occur For example, when youre working in Excel and you activate a different workbook, an Activateevent occurs