Your SlideShare is downloading. ×
Visual Basic For Applications Companion Slides
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Visual Basic For Applications Companion Slides

1,029
views

Published on

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

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,029
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
40
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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