Visual Basic For Applications Companion Slides

1,263 views

Published on

These are companion slides to the video at http://blip.tv/file/3011639

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,263
On SlideShare
0
From Embeds
0
Number of Embeds
218
Actions
Shares
0
Downloads
43
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Visual Basic For Applications Companion Slides

  1. 1. Visual Basic for Applications Companion slides to http://blip.tv/file/3011639 © The Stanford Decisions and Ethics Center decision.stanford.edu
  2. 2. On our agenda for today… <ul><li>Visual Basic for Applications </li></ul>
  3. 3. Buttons and Macros <ul><li>Lets create a simple button </li></ul>
  4. 4. Debugging <ul><li>MsgBox </li></ul><ul><li>Debug.print (Immediate Window – Ctrl+G) </li></ul>
  5. 5. Iteration Logic <ul><li>While Loops </li></ul><ul><li>Dim MyArray(5) As Double </li></ul><ul><li>For I = 1 To 5 </li></ul><ul><li>MyArray(I) = Rnd </li></ul><ul><li>Next I </li></ul><ul><li>For Each n In MyArray </li></ul><ul><li>Debug.Print n </li></ul><ul><li>Next n </li></ul>Array Indexing Starts from 1
  6. 6. Iteration Logic <ul><li>While-Wend Loops </li></ul><ul><li>Dim MyArray(5) As Double </li></ul><ul><li>I = 0 </li></ul><ul><li>While i<5 </li></ul><ul><li>MyArray(I) = Rnd </li></ul><ul><li> I = I+1 </li></ul><ul><li>Wend </li></ul><ul><li>For Each n In MyArray </li></ul><ul><li>Debug.Print n </li></ul><ul><li>Next n </li></ul>
  7. 7. Iteration Logic <ul><li>Do While </li></ul><ul><li>Do While condition </li></ul><ul><li>blah blah </li></ul><ul><li>Loop </li></ul><ul><li>Do </li></ul><ul><li>blah blah </li></ul><ul><li>Loop While condition </li></ul>
  8. 8. Iteration Logic <ul><li>Do Until </li></ul><ul><li>Do Until condition </li></ul><ul><li>blah blah </li></ul><ul><li>Loop </li></ul><ul><li>Do </li></ul><ul><li>blah blah </li></ul><ul><li>Loop Until condition </li></ul>
  9. 9. Conditional Logic <ul><li>Simple Conditionals </li></ul><ul><li>If condition_holds Then Do_something </li></ul>
  10. 10. Conditional Logic <ul><li>Nested Conditionals </li></ul><ul><li>If condition_holds Then Do_something </li></ul><ul><li>ElseIf another_condition_holds Then Do_something_else </li></ul><ul><li>End If </li></ul>
  11. 11. Conditional Logic <ul><li>More Nesting </li></ul><ul><li>If condition_holds Then Do_something </li></ul><ul><li>ElseIf another_condition_holds Then Do_something_else </li></ul><ul><li>ElseIf another_condition_holds Then Do_something_else </li></ul><ul><li>Else do_some_default_thing </li></ul><ul><li>End If </li></ul>
  12. 12. Example <ul><li>Armstrong Numbers </li></ul><ul><li>Sum of the cubes of the Armstrong number = number itself </li></ul><ul><li>E.g. 153 </li></ul><ul><li>1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 </li></ul>
  13. 13. Reading and Writing <ul><li>From and to cells </li></ul><ul><li>From and to cells in different worksheets </li></ul>
  14. 14. Functions and Subroutines <ul><li>Sub Blah() </li></ul><ul><li>End Sub </li></ul><ul><li>Function Blah() </li></ul><ul><li>Blah = … </li></ul><ul><li>End Function </li></ul>
  15. 15. Refactoring <ul><li>Any  fool  can write code that a computer </li></ul><ul><li>understands. Only a good programmer </li></ul><ul><li>can write code that a human understands. </li></ul><ul><li>Martin Fowler, in his classic, Refactoring </li></ul>
  16. 16. Refactoring <ul><li>is changing design without changing functionality </li></ul><ul><li>Important refactorings for VBA </li></ul><ul><li>Decompose into Small Sub/Functions </li></ul><ul><li>Rename to use Intention-Revealing Names </li></ul><ul><li>Main Motivation </li></ul><ul><li>ONCE and ONLY ONCE </li></ul>
  17. 17. Getting Inputs <ul><li>Dim Quantity As Variant </li></ul><ul><li>Dim Discount As Double </li></ul><ul><li>Quantity = InputBox(&quot;Enter Quantity: &quot;) </li></ul><ul><li>If Quantity = &quot;&quot; Then Exit Sub </li></ul><ul><li>If Quantity >= 0 Then Discount = 0.1 </li></ul><ul><li>If Quantity >= 25 Then Discount = 0.15 </li></ul><ul><li>If Quantity >= 50 Then Discount = 0.2 </li></ul><ul><li>If Quantity >= 75 Then Discount = 0.25 </li></ul><ul><li>MsgBox &quot;Discount: &quot; & Discount </li></ul>
  18. 18. Select Case <ul><li>Dim Quantity As Variant </li></ul><ul><li>Dim Discount As Double </li></ul><ul><li>Quantity = InputBox(&quot;Enter Quantity: &quot;) </li></ul><ul><li>Select Case Quantity </li></ul><ul><li>Case &quot;&quot; </li></ul><ul><li>Exit Sub </li></ul><ul><li>Case 0 To 24 </li></ul><ul><li>Discount = 0.1 </li></ul><ul><li>Case 25 To 49 </li></ul><ul><li>Discount = 0.15 </li></ul><ul><li>Case 50 To 74 </li></ul><ul><li>Discount = 0.25 </li></ul><ul><li>End Select </li></ul><ul><li>MsgBox &quot;Discount: &quot; & Discount </li></ul>
  19. 19. Reffering to cells <ul><li>Range(“name”).Value = NewValue </li></ul><ul><li>Range(“A5”).value = … </li></ul><ul><li>Cells(..,..).Offset(..,..).Value </li></ul><ul><li>ActiveCell.Offset(..,..).Value </li></ul><ul><li>Range(“..”).Offset(..,..).Value </li></ul><ul><li>ActiveSheet.Cells.ClearContents </li></ul>
  20. 20. Getting VBA Help <ul><li>Make sure the Help for VBA module is installed when installing Excel 2003 </li></ul><ul><li>Use ObjectBrowser </li></ul>
  21. 21. Book on VBA                                                             

×