Presentation is loading. Please wait.

Presentation is loading. Please wait.

University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.

Similar presentations


Presentation on theme: "University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming."— Presentation transcript:

1 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming Style Dialog Boxes (UserForm) Worksheet button Functions Spreadsheet vs VBA

2 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 2 Variable Scope Procedure Level (local) Dim iMarbles As Integer placement: within Sub and End Sub Module Level (global) Dim giTotal As Integer placement: before all Sub statements Project Level (public) Public piGrandTotal As Integer placement: before all Sub statements

3 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 3 Calling another subprogram Sub FirstOne() MsgBox “In 1st subprogram…” SecondOne End Sub Sub SecondOne() MsgBox “In 2nd subprogram…” End Sub

4 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 4 Programming Style Good style improves readability of code and your program is easier to maintain! 1. Comments (documentation)  at top (your name, date, etc)  each subprogram  each variable  each major section of code Don’t restate the obvious! Dim iCount As Integer ‘ declare integer variable

5 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 5 2. Good variable names  describe what variables store  not too long, or too short 3. Indentation  subprograms and if statements 4. Constants  for values that do not change 5. White space  leave the occasional blank line Programming Style

6 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 6 6. Minimize variable scope  only make a global or public variable if needed 7. Declare and Initialize variables  Option Explicit forces variable declaration  set initial value of variables appropriately Programming Style

7 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 7 Programming Style - example

8 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 8 Programming Style - example

9 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 9 UserForm Custom dialog box Example:  create UserForm to collect personal info  form will be launched from Excel  data entered in form is stored in Excel

10 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 10 Insert blank form Insert  UserForm blank UserForm Toolbox with control objects

11 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 11 Design form Add control objects to form Label (display text) TextBox (for input) CommandButton (perform an action)

12 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 12 Initialize properties Initialize properties of controls Label (Name): lbl… Caption: TextBox (Name): txt… Text: CommandButton (Name): cmd… Caption:

13 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 13 Write code Determine events to which VBA should respond (eg. clicking Add CommandButton) double-click Add button to open Code window and type the code Private Sub cmdAdd_Click() code goes here... End Sub

14 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 14 Flowchart Start Name entered? Stop Separate name into first and last Store in worksheet: last, first, phone Clear textboxes on form Report error Phone entered? No Yes

15 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 15 refers to textbox contents Code

16 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 16 Code

17 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 17 Create macro How can we start this form from Excel? There is no macro listed! write a macro to display form form doesn’t appear until called put this code in Module code, not Form code: Sub EnterPersonalInfo ()macro name UserForm1.Show form name End Sub

18 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 18 Add worksheet button Optional: button on worksheet to run macro >View >Toolbars >Drawing >Oval icon right-click Add Text… right-click Assign Macro… >View >Toolbars >Control >CommandButton icon right-click Properties right-click View Code >Exit Design Mode icon

19 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 19 Excel Functions Most worksheet functions have a VBA equivalent In VBA: Application.WorkSheetFunction._ FunctionName(arguments) Application.WorksheetFunction.Today() Application.WorksheetFunction.IsNumber (Range(“B3”)) In Excel: =FunctionName(arguments) =Today() =IsNumber(B3)

20 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 20 Find Function VBA Function – not available on sheet Function is called on an object that defines a spreadsheet range  E.g., Selection.Find(“Tom”) Returns range location of first match, or returns nothing if no match found. Range object need not be sorted

21 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 21 Macro asks for name to find and stores in E8 Find function locates name, uses offset to get its age vName = InputBox(“Name?”, “Enter name”) vAge = Range("A4:A10").Find(vName). Offset(0, 2).Value Find Function

22 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 22 VLookUp Function Vlookup (value, table, col_num, close)  value: item to find  table: range of data to search must be sorted by 1 st column  col_num: return data from which column?  close: true or false True: select nearest match – always finds False: find exact, or return #N/A

23 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 23 How it works in Excel VLookUp Function

24 University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 24 How it works in VBA vName = InputBox(“Name?”, “Enter name”) Range(“E8").Value = vAge vAge = Application.WorksheetFunction. _ VLookup(Range("E8"), Range("A4:C10"), 3, True) Range("F8").Value = vAge VLookUp Function


Download ppt "University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming."

Similar presentations


Ads by Google