# Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about

Post on 30-Dec-2015

212 views

Embed Size (px)

TRANSCRIPT

Class 3Programming in Visual Basic

Class Objectives

Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about Boolean operations

Addressing Spreadsheet CellsRowsColumnsCell C2A1 Reference Style

Addressing Spreadsheet CellsRowsCell (2, 3)R1C1 Reference Stylerow, columnColumns

VBA Interaction with ExcelExcelVBA Subroutine0, 1, 2, 3 numbers1, 2, 3 numbersOur focus today

StringsStringsin double quotesNo arguments passed

Results

Sample Subroutine with CalculationSub sum_and_product_of_five_numbers() 'This program gives the sum and product of 'five numbers located in Cells B1 to B5 Dim row As Integer Dim sum, product As Double sum = 0 product = 1 For row = 1 To 5 sum = sum + Cells( row, 2) product = product * Cells( row, 2) Next row Cells(6, 1) = "sum" Cells(7, 1) = "product" Cells(6, 2) = sum Cells(7, 2) = productEnd Sub Subroutine name

Comment

Declare variables

Initialize variables

Start loop

Input and calculate

End loop

Output

EndIn VBA editor, enter this program.

Example (cont)Enter fivenumbers incells B1 to B5

Example (cont)Press Alt+F8

Select

Press Run

Result

An easier way to execute a subroutineYou can link a subroutine to a keyboard control character.Assignment is made under Options in the Macro dialog box.

Using Macro OptionsUnder Options, fill in a letter next to ctrl+ and the selected macro can be executed by pressing that key combination.Be careful. If you choose a character that is a standard Windows function (e.g., ctrl+c), VBA will overwrite the Windows function with your macro.

ArraysDim list (5) as integerExample 10 4 1 7

2 89

3 8

4 45

5 -3IndicesDefault: Index starts with zeroNumbersstoredinarray

ArraysDim list (1 to 5) as integer 1 7

2 89

3 8

4 45

5 -3IndicesOverride: Index starts with oneNumbersstoredinarrayExample 2

ArraysDim list (1 to 5, 1 to 2) as integer 1 7 32

2 89 9

3 8 46

4 45 -8 5 -3 8Row Indices1 2Column IndicesThis is a two-dimensional array. Up to 60 dimensions are allowed.Example 3

Sample ProgramSub array_demo() Dim list(1 To 5) As Integer Dim i As Integer Dim sum As Integer sum = 0 i = 1 list(1) = 1 list(2) = 4 list(3) = 9 list(4) = 16 list(5) = 25 Do While sum < 16 sum = sum + list( i) i = i + 1 Loop Cells(15, 1) = "sum =" Cells(15, 2) = sumEnd SubTitle

Declare variables

Initialize variables

Start loop

Calculate

End loop

Output

EndEnter and run this program.

ResultsSub array_demo() Dim list(1 To 5) As Integer Dim i As Integer Dim sum As Integer sum = 0 i = 1 list(1) = 1 list(2) = 4 list(3) = 9 list(4) = 16 list(5) = 25 Do While sum < 16 sum = sum + list( i) i = i + 1 Loop Cells(15, 1) = "sum =" Cells(15, 2) = sumEnd Sub

Boolean OperationsTruth Tablesresult = expression1 Or expression2result = Not expressionresult = expression1 And expression20 = false1 = trueY A B

Example ProgramOption ExplicitSub Boolean_test() Dim truth As Boolean Dim a, b As Integer a = 1 b = 2 truth = False If a = 1 And b = 2 Then truth = True End If Cells(1, 1) = truthEnd SubTitle

Declare variables

Initialize

Boolean operationAssign

OutputEndEnter and run this program. To see what happens, play with various Boolean operators, and different initial values of a and b.

Function ExerciseThe value of p (the ratio of a circles circumference to its diameter) has been estimated for about 4000 years.Several methods for calculating p are presented in the Math Supplement (pages 8-10).Excels built in function for p is only accurate to 14 decimal places (van Rooman did better in 1593).As a team, pick ~4 (1 per team member) of the various methods used to estimate p using infinite series (avoid Methods a, c, e and k).Program your function to calculate the first 50 terms, or stop when the value does not change by more than 10-6.

Subroutine ExerciseConvert your function into a subroutine.The subroutine should title Column A as p and Column B as D, that is, the change in p from the previous iteration. Then beginning in Cells A2 and B2 and going down, output the successive terms of p.Stop the iteration after the first 200 terms, or if an additional term does not change the value of p by more than 10-10.For each method selected by your teammates, compare the rate that they converge to an accurate value of p.

Recommended