240 likes | 405 Views
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. Procedures. Subroutines (Subs)
E N D
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
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.
If…Then • If…Then has two basic forms: • Statement If (x = 1) Then Debug.Print "Yes" • Code Block If (x = 1) Then y = 1 Else y = -1 End If
For…Next • Loops through a range of values and executes the statements in the block.
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.
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
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
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
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)
Ranges: Writing Code Sub SimplestHello() Range("A1") = "Hello" Range("A2", "B4") = "Goodbye" End Sub
Ranges: Using a Range Variable Sub HelloRange() Dim a1 As Range Set a1 = Range("A1") a1 = "Hello world!" End Sub
Instead of... Range("A1") Sheets("Sheet1") Charts("Chart1") Use Selection or ActiveCell ActiveSheet ActiveChart Specific -> Generic
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
Collection: Example Sub AddWorksheet() Dim wks As Worksheet Set wks = Worksheets.Add MsgBox "Added: " + wks.Name End Sub
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 Next End Sub