Unit 7: Built-In Functions


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Unit 7: Built-In Functions

  1. 1. Excel Macros Level 1<br />Built-in Functions and Statements<br />
  2. 2. Built-in Functions<br />VBA has large number built-in functions and statements<br />Some are shown in Table 7-1 (p. 78) and Table 7-2 (p. 80)<br />4/29/2010<br />M. Campbell - 2010<br />2<br />p. 78<br />
  3. 3. Procedure Template<br />SubChangeFormat(FontName [, FontSize])<br />Parameters listed in italics<br />Optional parameters listed in [square brackets]<br />Type declarations will be listed separately<br />4/29/2010<br />M. Campbell - 2010<br />3<br />p. 78<br />
  4. 4. MsgBox<br />MsgBox(prompt [, buttons] [, title])<br />prompt (String)<br />Message to display in body<br />buttons (Long)<br />Sum of values to specify properties of message box<br />See Table 7-2 for enum of argument values<br />title (String)<br />Message to display in title bar<br />4/29/2010<br />M. Campbell - 2010<br />4<br />p. 78<br />
  5. 5. MsgBox<br />MsgBox function returns number indicating which button pressed<br />Defined as VbMsgBoxResult enumerator<br />See Table 7-3 for return values<br />4/29/2010<br />M. Campbell - 2010<br />5<br />p. 78<br />
  6. 6. Activities<br />In the Unit 7 Activities complete:<br />Activity 1: MsgBox Procedure<br />4/29/2010<br />M. Campbell - 2010<br />6<br />
  7. 7. InputBox<br />InputBox(prompt[, title] [, default])<br />prompt (String)<br />Message to display<br />title (String)<br />Message to display in title bar<br />default (String)<br />Default value for the text box<br />4/29/2010<br />M. Campbell - 2010<br />7<br />p. 81<br />
  8. 8. InputBox<br />Returns the string the user enters in text box<br />title (String)<br />Message to display in title bar<br />default (String)<br />Default value for the text box<br />4/29/2010<br />M. Campbell - 2010<br />8<br />p. 81<br />
  9. 9. Activities<br />In the Unit 7 Activities complete:<br />Activity 2: InputBox Subroutine<br />4/29/2010<br />M. Campbell - 2010<br />9<br />
  10. 10. VBA String Functions<br />Please review pages 82-84 especially:<br />Len<br />UCase and LCase<br />Left, Right, and Mid<br />Str and Val<br />Trim, LTrim, and RTrim<br />4/29/2010<br />M. Campbell - 2010<br />10<br />p. 82<br />
  11. 11. Activities<br />In the Unit 7 Activities complete:<br />Activity 3: Phone Number Processing<br />4/29/2010<br />M. Campbell - 2010<br />11<br />
  12. 12. The Is Functions<br />IsDate<br />Returns true if variable is a date<br />IsEmpty<br />Returns true if variable is empty<br />IsNull<br />Returns true if variable is Null<br />IsNumeric<br />Returns true if variable is a numeric type<br />4/29/2010<br />M. Campbell - 2010<br />12<br />p. 85<br />
  13. 13. Immediate If Function<br />IIf(Expression, TruePart, FalsePart)<br />If Expression is True returns TruePart<br />If Expression is False returns FalsePart<br />Always evaluates both parts<br />Can lead to errors if one part should not be evaluated<br />4/29/2010<br />M. Campbell - 2010<br />13<br />p. 85<br />
  14. 14. Switch Function<br />Switch(expr1, value1, expr2, value2, _<br /> … , exprn, valuen)<br />When it finds an expression that matches it returns the corresponding value<br />If no match is made returns Null<br />4/29/2010<br />M. Campbell - 2010<br />14<br />p. 86<br />
  15. 15. Dim number As Variant<br />Dim check As Integer<br />number = Switch(check = 1, "one", _<br /> check = 2, "two", _<br /> check = 3, "three")<br />If Not IsNull(number) Then<br />Debug.Print(number)<br />Else<br />Debug.Print("Unrecognized number")<br />End If<br />4/29/2010<br />M. Campbell - 2010<br />15<br />
  16. 16. Units Conversion<br />InchesToPoints(inchValue)<br />PointsToInches(pointValue)<br />Many Excel functions take values in points<br />May prefer to work in inches<br />72 points in an inch<br />4/29/2010<br />M. Campbell - 2010<br />16<br />p. 87<br />
  17. 17. Handling Errors<br />VBA has methods to handle run-time errors<br />Error handling is vital in applications meant for public release<br />4/29/2010<br />M. Campbell - 2010<br />17<br />p. 88<br />
  18. 18. On Error Goto<br />On Error GotoLabel<br />If a run-time error occurs VBA will goto the code following the label<br />4/29/2010<br />M. Campbell - 2010<br />18<br />p. 88<br />
  19. 19. On Error Resume Next<br />On Error Resume Next<br />VBA will continue executing code immediately following line that caused error<br />4/29/2010<br />M. Campbell - 2010<br />19<br />p. 90<br />
  20. 20. Resume Statement<br />Resume<br />Resume with line that caused error<br />Good if your code corrected the error<br />Resume Next<br />Resume with the line after the one that caused error<br />Resume label<br />Resume with code at line labelled label<br />4/29/2010<br />M. Campbell - 2010<br />20<br />p. 91<br />