24
VBA Programming Session #2

VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next

  • View
    222

  • Download
    1

Embed Size (px)

Citation preview

Page 1: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

VBA Programming

Session #2

Page 2: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Things to Review Variables Procedures: Subs & Functions If…Then For…Next

Page 3: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Variables Just like in math, variables:

Have a name Have a value Value can be changed

Examples: x = 1 : debug.print x x = 2 : debug.print x

Page 4: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Procedures Subroutines (Subs)

Can do things but not return values. Subs without arguments can be called from the

Excel macro toolbar. Functions

Functions return values in VBA with the form: FunctionName = SomeValue

Functions can be inserted into Excel sheets from the Insert|Function menu.

Page 5: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

If…Then If…Then has two basic forms:

StatementIf (x = 1) Then Debug.Print "Yes"

Code BlockIf (x = 1) Then y = 1Else y = -1End If

Page 6: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

For…Next Loops through a range of values and executes

the statements in the block.

For x = 1 To 10 Debug.Print xNext

1 2 3 4 5 6 7 Etc.

Page 7: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

New Stuff

Page 8: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

VBA Variable Types The most commonly used VBA types are:

Boolean (True or False) Numeric Types: Integer, Double, Currency Date String Variant (holds almost anything) Object Types: Ranges, Worksheets, etc.

Page 9: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Objects A combination of code and data that can

be treated as a unit, for example, a control, form, or application component. Each object is defined by a class. (MS)

Examples: Workbooks Worksheets Charts Ranges

Page 10: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Declaring Variables Declaring means:

Telling VBA that you are going to use it Defining the variable's type

Declare variables using the Dim statement Dim MyName as String Dim MyBirthday as Date Dim BigMoney as Currency Dim wks as Worksheet

Page 11: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Assigning Values to Variables Most variables can simple be assigned like

this: X = 5 MyName = "Bob"

Objects need to be assigned using Set Set MyDataSheet = ActiveSheet Set MyChart = ActiveChart

Page 12: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Forgot to set?

Page 13: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Range Object Represents a cell, a row, a column, a selection of

cells containing one or more contiguous blocks of cells, or a 3-D range. (MS)

Page 14: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Understanding Ranges

Page 15: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Ranges: Writing CodeSub SimplestHello() Range("A1") = "Hello" Range("A2", "B4") = "Goodbye"End Sub

Page 16: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Ranges: Using a Range VariableSub HelloRange() Dim a1 As Range Set a1 = Range("A1") a1 = "Hello world!"End Sub

Page 17: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Offsets: If only there was just "A1"

Page 18: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Combining it all... Data Table

Page 19: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

The Macro Recorder: Part II

Page 20: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Specific -> Generic Instead of...

Range("A1") Sheets("Sheet1") Charts("Chart1")

Use Selection or ActiveCell ActiveSheet ActiveChart

Page 21: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Collection Object Most of the VBA objects you use are parts

of collections. For example: Charts: a collection of Chart objects Worksheets: a collection of Worksheet objects

Collections have the following: Count: number of items in the collection Add: add an item Item: get the value of an item Remove: delete item

Page 22: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Collection: Example

Sub AddWorksheet() Dim wks As Worksheet Set wks = Worksheets.Add MsgBox "Added: " + wks.NameEnd Sub

Page 23: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Looping Through Collections Collections are handy to use because we

can easily look at each item in the collection using a "For Each" loop.

For example:

Sub ListWorksheets() Dim wks As Worksheet For Each wks In ActiveWorkbook.Worksheets MsgBox wks.Name NextEnd Sub

Page 24: VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Collections: Exercise