Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7.  Recap  Input Validation Message Boxes Input Boxes  Conversion Functions  Assessment 2.

Similar presentations


Presentation on theme: "Week 7.  Recap  Input Validation Message Boxes Input Boxes  Conversion Functions  Assessment 2."— Presentation transcript:

1 Week 7

2  Recap  Input Validation Message Boxes Input Boxes  Conversion Functions  Assessment 2

3  Do Loops  Custom Functions

4 Function My_Func() Application.Volatile '' Remainder of the function “ End Function

5 Do loops with input validation

6 ConstantValueDescription vbOKOnly0OK button only (default) vbOKCancel1OK and Cancel buttons vbAbortRetryIgnore2Abort, Retry, and Ignore buttons vbYesNoCancel3Yes, No, and Cancel buttons vbYesNo4Yes and No buttons vbRetryCancel5Retry and Cancel buttons vbCritical16Display Critical Message icon MsgBox(prompt, buttons, title, helpfile, context)

7  The return value for the Input Box function returns a string by default. Testing for an empty string S = inputbox(“typesomething”) If s <> “” then MsgBox “You typed nothing” End if

8  The Type argument of an input box allows you to specify other data types (eg dates)  However, this requires the user to ensure the data entered is of the specified type and can lead to errors  Instead, use the default string input and then test it for type and convert is as necessary

9 dim date1 as Date date1 = inputBox ("type a date") If the user does not type a date, the macro dies ignominiously. dim int1,int2,answer as Integer int1 = inputbox("please type a whole _ number") int2 = inputbox("please type another whole _ number") answer = int1 + int2 If the user types a non-integer value, then the macro dies and gives an unfriendly error message.

10  The following functions test what a variable contains: isdate(variable) isnumeric(variable) isempty(variable) ismissing(variable) isnull(variable)  where variable is any variable where the value could be what is tested for. They are Boolean functions, in that they return either True or False

11 cDate(E)converts a compatible expression to a date cInt(E)converts a compatible expression to an integer cDbl(E)converts a compatible expression to a double cLong(E)converts a compatible expression to a long cStr(E)converts a compatible expression to a string  Once you have validated your input you will need to convert it to the correct data type  Find out more about these functions at http://www.ozgrid.com/VBA/conversion-functions.htm

12 Sub date_unvalidated() Dim d As Date d = InputBox("type a date") Range("A1").Value = Now Range("A2").Value = d Range("A3").Value = Now - d End Sub Sub date_validated() Dim d As Date Dim s As String s = InputBox("type a date") If Not IsDate(s) Then MsgBox "No date, stupid" Else d = CDate(s) Range("A1").Value = Now Range("A2").Value = d Range("A3").Value = Now - d End If End Sub

13 Sub numbers_unvalidated() Dim n As Integer n = InputBox("type a number") Range("b1").Value = 1000 Range("b2").Value = n Range("b3").Value = Range("A1").Value- _ Range("A2").Value Range("b3").NumberFormat = "0.00;[Red]0.00" End Sub 

14 Sub numbers_validated_not_isnumeric() Dim n As Integer Dim s As String s = InputBox("type a number") If Not IsNumeric(s) Then MsgBox "No number, stupid" Else n = CInt(s) Range("b1").Value = 1000 Range("b2").Value = n Range("b3").Value = Range("A1").Value - Range("A2").Value End If End Sub

15 Alternatively, use isnumeric without not Sub numbers_validated_isnumeric() Dim n As Integer Dim s As String s = InputBox("type a number") If IsNumeric(s) Then n = CInt(s) Range("b1").Value = 1000 Range("b2").Value = n Range("b3").Value = Range("A1").Value -Range("A2").Value Else MsgBox "No number, stupid" End If End Sub

16  You can test for empty cells using the following: if isempty(acell) Then --- do something ---

17  Controls  Modifying toolbars etc


Download ppt "Week 7.  Recap  Input Validation Message Boxes Input Boxes  Conversion Functions  Assessment 2."

Similar presentations


Ads by Google