VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then ...
-
date post
21-Dec-2015 -
Category
Documents
-
view
219 -
download
1
Transcript of VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then ...
![Page 1: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/1.jpg)
VBA Programming
Session #2
![Page 2: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/7.jpg)
New Stuff
![Page 8: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/12.jpg)
Forgot to set?
![Page 13: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/14.jpg)
Understanding Ranges
![Page 15: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/17.jpg)
Offsets: If only there was just "A1"
![Page 18: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/18.jpg)
Combining it all... Data Table
![Page 19: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/19.jpg)
The Macro Recorder: Part II
![Page 20: VBA Programming Session #2. Things to Review Variables Procedures: Subs & Functions If…Then For…Next.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/23.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649d605503460f94a41520/html5/thumbnails/24.jpg)
Collections: Exercise