Вы находитесь на странице: 1из 26

# Part I: Closed book.

## 20 points (Answers are in RED)

1. Provide the formula (summation form) for Variance

Variance =

)^2 / (n)

## Dim Xi(1 to 10), average as double, i as integer

With range (A1)
Average
For i = 1 to n
total = total +.cells(i,1)
Next I
average = total / n
total = 0
For i = 1 to n
total = total + (.cells(i,1)-average)^2
Next i
VarNce=total/n
2. Provide the EXCEL syntax for calculating the standard deviation for the values in range A1:A20 STDEVA(A1:A20)
OR STD(A1:A20)
3. You have the VBA code: Dim Scores (1 to 15) as Integer. How do you refer to the j+5th element in Scores
Scores(j+5)
4. Provide an example of VBA syntax for a Do Until loop:
Instructors example:
Do Until iCount > 5
Sum = sum + 3
Loop
Do until num = 10
Num = num +1
loop
Our example:
Dim i As Integer
i=1
Do Until i > 6
Cells(i, 2) = Cells(i, 1).Value
i=i+1
Loop
or
Dim total as integer, i as integer
total = 0
i=1

## With Range (A1)

Do Until total > 50
total = total + .cells(i,1)
i = i +1
Loop
5. Provide an example of the VBA syntax for two nested For Next loops
Instructors example:
For I = 1 to m
For j = 1 to 4
Msgbox HellO
Next J
Next I
Our example:
For i = 1 to range
total = 0
for j = range to i step -1
total = total + .cells(j,1)
next j
.cells(i,2) = total
next i
For i = 1 to 2
For j = 1 to 2
Msgbox Whatever
Next j
Next i
6. Provide the VBA syntax for defining a function named Adjust that takes two values (both double) and returns a
double value.
Function Adjust(x As Double, y as Double) as double
End Function

## 7. Using an EXCEL formula for generate a random number between 1 and 6

=Int(rand()*6)+1
or
=randbetween(1,6)
8. Provide an example of the the VBA syntax for the IF Then Else statement
If Numshots > 5 then
Msgbox ouch
Else
Msgbox Okay
End if
if found = true then
Msgbox(Customer is found)
Else
End if

if x>2 then _
y=2
Else
y=1
9. Provide an example of the EXCEL syntax for using NPV
NPV(0.06, 100,200, 100, 300)
10. If cell G3 is less than 50, set cell G3 to the value of cell F2 times 0.95 otherwise set it equal to the value of cell
F2 times 1.1. Provide the syntax to do this in Excel using an IF statement =IF(G3 < 50, F2*0.95, F2*1.1)
Part II: Open book. No Computer or Calculator. 40 Points
NOTE: YOU Wont be able to use the computer for this section.
Understanding code: Select a total of five questions. As directed.
Assume the following data values on the spreadsheet

OrderID

ProductID

UnitPrice

Quantity

Discount

QOH

Difference

10248

11

14.00

12

42

10248

42

9.80

10

10248

72

34.80

59

10249

14

18.60

38

10249

51

42.40

40

48

10250

41

7.70

10

62

10250

51

42.40

35

0.15

10

10250

65

16.80

15

0.15

10

10251

22

16.80

0.05

18

11

10251

57

15.60

15

0.05

59

12

10251

65

16.80

20

29

13

10252

20

64.80

40

0.05

76

14

10252

33

2.00

25

0.05

48

15

10252

60

27.20

40

88

16

10253

31

10.00

20

14

17

10253

39

14.40

42

60

18

10253

49

16.00

40

19

10254

24

3.60

15

0.15

72

20

10254

55

19.20

21

0.15

26

21

10254

74

8.00

21

54

22

10255

15.20

20

45

23

10255

16

13.90

35

49

24

10255

36

15.20

25

63

25

10255

59

44.00

30

77

26

10256

53

26.20

15

54

27

10256

77

10.40

12

28

10257

27

35.10

25

23

29

10257

39

14.40

12

30

10257

77

10.40

15

13

31

10258

15.20

50

0.2

23

32

10258

17.00

65

0.2

33

10258

32

25.60

0.2

10

34

10259

21

8.00

10

18

35

10259

37

20.80

62

36

10260

41

7.70

16

0.25

32

37

10260

57

15.60

50

83

38

10260

62

39.40

15

0.25

62

39

10260

70

12.00

21

0.25

23

Given the following code determine is the outcome or action? Write your answer in the space provided You will get
five questions total.
Do 1 Assume D = Range(D4).value, A = Range(D32).Value
1. Using the data above (table)
ACount = 0

BCount = 0
ATotal = 0
BTotal = 0
n=8
D = Range(D4).value = 5
A = Range(D32).Value = 65
F = (A-D)/2 = (65-5)/2 ; F = 30
For j = 1 to n ; For j = 1 to n ; n = 8
D = Range(D3).Cells(j, 1) D = 10
If D > F then IF 10 > 30
ACount = ACount +1 Then Acount = Acount + 1
ATotal = D + ATotal Atotal = D + atotal
Else ELSE
BCount =BCount +1 BCcount= Bcount+ 1
BTotal = D + BTotal Btotal = D + Btotal
End If
Next; Next J
Value of ACount is: 2
Value of ATotal is: 75
Value of BCount is: 6
Value of BTotal is: 55
Sub we()
acount = 0
BCount = 0
atotal = 0
BTotal = 0
n=8
D = Range("D4").Value
A = Range("D32").Value
F = (A - D) / 2
For j = 1 To n
D = .)
If D > F Then
acount = acount + 1
atotal = D + atotal
Else
BCount = BCount + 1
BTotal = D + BTotal
End If
Next
End Sub
2. Use data table above
With Range(D2)
For i = 1 to 5
.cells(i,4) = .cells(i, 3)-.cells(i,1)
Next
End With
I=1
.cells(1,4)= .cells(1,3) - cells(1,1) ;
30=42-12

I=2
.cells(2,4) = .cells(2,3)-.cells(2,1)
-5 = 5 - 10
I=3
.cells(3,4) = .cells(3,3)-.cells(3,1)
54=59-5
i=4
.cells(4,4)=.cells(4,3)-.cells(4,1)
29=38-9
i=5
.cells(5,4)= .cells(5,3)-.cells(5,1)
8=48-40

## 3. Dim QOH(1 to 10) as integer

For k = 1 to 10
QOH(k) = Range(F2).Cells(k,1)
Next k
Total = 0
For i = 1 to 10
If QOH(i) > 30 then
Total = Total + QOH(i)
End if
Next i
For k = 1 to 10
QOH(k) = Range(F2).Cells(k,1)
QOH(1)=42
QOH(2)= 5
QOH(3)=59
QOH(4)=38
QOH(5)=48
QOH(6)=62
QOH(7)=10
QOH(8)=9
QOH(9)=18
QOH(10)=59
Next K
Total = 0
For i = 1 to 10
If QOH(i) > 30 then
Total = Total + QOH(i)
Values of QOH array=
QOH (1) = 42 ; 42 > 30
Total = 0 + 42 = 42
QOH(2) = 5 < 30
Total = 42
QOH(3) = 59 > 30
Total = 42+59 = 101

QOH(4) = 38 > 30
Total = 101 + 38 = 139
QOH(5) = 48 > 30
Total = 139+48 = 187
QOH(6) = 62 > 30
Total = 187+62 = 249
QOH(7) = 10 < 30
Total = 249
QOH(8) = 9 < 30
Total = 249
QOH(9)=18 < 30
Total = 249
QOH(10)= 59 > 30
Total = 308

4.
Mult = 1
For i = 1 to 3
For j = 1 to 2
Mult = i * j * Mult
Msgbox i & times & j is & Mult
Next j
Next i
i = 1, j = 1, mult = 1 ; 1 times 1 is 1
i = 1, j = 2, mult = 1*2*1=2; 1 times 2 is 2
i = 2, j = 1, mult = 2*1*2 = 4; 2 times 1 is 4
i = 2, j = 2, mult = 2*2*4 = 16; 2 times 2 is 16
i = 3, j = 1, mult = 3*1*16=48; 3 times 1 is 48
i = 3, j = 2, mult = 3*2*48= 288; 3 times 2 is 288

## 5. Using the data above

Dim OrdNum(1 to 6) as integer
Products(1 to 6) as integer
With Range("A2")
Avg = 60
For i = 1 To 51
OrdNum(i) = .cells(i, 1)
Products(i) = .cells(i, 2)
Next
For i = 1 To 5
If Products(i) > Avg then
Msg= Order & OrdNum(i ) & has product numbered & Products(i)
Msgbox Msg & that is greater than 10
End if
Next
End With
What is the output?

OrdNum(1)= 10248
OrdNum(2)= 10248
OrdNum(3)= 10248
OrdNum(4)= 10249
OrdNum(5)= 10249
Products(1)=11
Products(2)=42
Products(3)=72
Products(4)=14
Products(5)=51
Output is: Order 10248 has product numbered 72 that is greater than 10

## Part III 40 points (1-4 5 points, 5-6 are 10 points)

This part is open book, notes, computer. DO ALL SIX QUESTIONS
ASSUME OPTION EXPLICIT AND OPTION BASE 1
DO 1
1. Write a function that returns the discounted cash flow given the interest rate, the Time period in years, and the
cash flow based on the discount value = 1/(1+rate)^Timeperiod
Option Explicit
Option Base 1
Function discVal(r as double, n as Integer, cFlow as double) as double
Dim disc as double
disc =1/ (1+r)^n
discVal = cFlow * disc
End Function
Example 2:
Function discVal(r as double, n as integer, cshFlow as double) as double
discVal=cshFlow*dscR(r,n)
End function
Function discR(r as double, N as integer) as double
discR=1/(1+r)^n
End function
2. Assume that the data is stored in spreadsheet in three columns. The first column has the team names, the
second column has the team member and the third has the team roles. Write a sub that displays all the team
members and their roles based on team name which the user provides. There can be any number of rows. See the
sample output below. You can use your own data.

Sub teamname()
Dim searchname As String, nrows As Integer
searchname = InputBox("enter a name")
Call search(searchname, nrows)
End Sub
Sub search(searchname As String, nrows As Integer)
Dim found As Integer
With Range("A1")
nrows = Range(.Offset(0, 0), .End(xlDown)).Rows.Count
found = 0
For i = 1 To nrows
If .Cells(i, 1) = searchname Then
MsgBox .Cells(i, 2).Value & " " & .Cells(i, 3).Value
found = 1
End If
Next i
If found = 0 Then
MsgBox ("There isn't any names with your search")
End If
End With
End Sub
3. Assume you have two strings passed to the sub both of the same length. Write a Sub that creates a series of new
strings that are made up of the other two strings where the characters are alternated and displays each new string to
the user. For instance, if you had string Quandry and string entered. The output would be:

## The new string is: Qe

The new string is: Qeun
The new string is: Qeunat
The new string is: Qeunatne
The new string is: Qeunatnedr
The new string is: Qeunatnedrre
The new string is: Qeunatnedrreyd
Hint: Concatenation: astring = astring & some text
Hint: Len(string) length of string
Hint: Mid(string, start, length of substring).
Quandry - entered
Sub mixemUp(str1 as string, str2 as string)
n = Len(str1) n = 7
astring =
For i = 1 to n
astring = astring & mid(str1, i, 1) & mid(str2, i, 1)
Msgbox The new string is: & astring
Next i
End Sub
4. Write a sub that computes the total and averages for each row and column of a table where the data starts in B2
on worksheet Operations.
Assume five rows and columns with the following data:
Customer ID

Jan

Feb

Mar

Apr

May

Row totals

1001

10

12

17

15

15

69

1002

20

22

19

18

21

100

1003

14

13

12

15

16

70

1004

15

16

11

10

14

66

1005

18

19

22

23

17

99

Col Totals

77

82

81

81

83

404

Note: I have included the row and column totals, but your sub should do that.
Instructors solution:
With Range(B2)
n=5
gtotal = 0
for k= 1 to n
total=0
for j= 1 to n
total=total+.cells (j,k)
Next j
.cells (n+1,k)=total
gtotal = gtotal + total
next k
For j=1 to n

Total=0
for k= 1 to n
total= total + .cells (j,k)
next k
.cells(j, n+1)=total
next j
.cells(n+1,n+1)=gtotal
End with
Dats solution:
Sub count()
With Range("b2")
nrows = Range(.Offset(0, 0), .End(xlDown)).Rows.count
ncols = Range(.Offset(0, 0), .End(xlToRight)).Columns.count
Total = 0
c=0
d=0
For j = 1 To ncols
For i = 1 To nrows
Total = Total + .Cells(i, j)
Next i
.Cells(i, j) = Total
c = c + Total
Total = 0
Next j
For i = 1 To nrows
For j = 1 To ncols
total1 = total1 + .Cells(i, j)
Next j
.Cells(i, j) = total1
d = d + Total
total1 = 0
Next i
.Cells(i, j) = c + d
End With
End Sub
5. (NOT ON THE EXAM)You have two lists of customers: those who bought last year and those who bought this
year. Each of these lists is in alphabetical order (by last name of customer). We'll assume that if two last names in
the database are identical in the database then it is a duplicate. For example, if you see Smith on last year's list and
this year's list, you can assume that it is the same Smith in both cases. So you only copy it once. You want to create
a new list that merges the names from these two lists into a common customer list. Write a sub that merges the two
lists. The lists are on columns A and B and can be of different lengths. The sample data below demonstrates the
outcome.
HINT: This requires determining the number of rows for the two columns. You will use and if statement to compare a
name in one column to a name in the other column. The one that is less than goes into the third column and you look
at the next names in each column.
Customers last
year

Customers this
year

Customers

Barlog

Aghimien

Aghimien

Barnett

Bang

Bang

Bedrick

Barnett

Barlog

Brulez

Bedrick

Barnett

Brulez

Bedrick

Castleman

Brulez

Chandler

Castleman

Chen

Chandler

Castleman

Cheung

Cheung

Chandler

Chong

Chong

Chen

Chou

Cochran

Cheung

Darbro

Cohn

Chong

Dewi

Darbro

Chou

Dove

Dewi

Cochran

Dudley

Dove

Cohn

Eastman

Dudley

Darbro

Option Explicit
Sub newnewnw()
Dim nrows As Integer, ncols As Integer, j As Integer, i As Integer
Dim ws As Worksheet
Dim rstr
Dim r As Range
With Range("a2")
nrows = Range(.Offset(0, 0), .End(xlDown)).Rows.Count
ncols = Range(.Offset(0, 0), .End(xlToRight)).Columns.Count
For j = 1 To ncols
For i = 1 To nrows
If j = 1 Then
.Cells(i, 3) = .Cells(i, 1)
Else
.Cells(i + 15, 3) = .Cells(i, 2)
End If
Next i
Next j

Set ws = ThisWorkbook.Worksheets("Sheet1")
' put the array values on the worksheet
rstr = "c1:C32"
Set r = ws.Range(rstr)
' sort the range
r.Sort key1:=r, order1:=xlAscending, MatchCase:=False
removing duplicates by setting range and columns=array
End With
End Sub
6. Write a sub that computes the overall average for the data give in 4 and then generates another table of values on
the spreadsheet that is the difference between each cell in the spreadsheet and the average. The sheet below
shows you what the results will be.
Customer ID

Jan

Feb

Mar

Apr

May

Row
totals

## Difference between data and the average

of 16.16

1001

10

12

17

15

15

69

-6.16

-4.16

0.84

-1.16

-1.16

1002

20

22

19

18

21

100

3.84

5.84

2.84

1.84

4.84

1003

14

13

12

15

16

70

-2.16

-3.16

-4.16

-1.16

-0.16

1004

15

16

11

10

14

66

-1.16

-0.16

-5.16

-6.16

-2.16

1005

18

19

22

23

17

99

1.84

2.84

5.84

6.84

0.84

Col Totals

77

82

81

81

83

404

Instructors solution:
For i = 1 to n
For k = 1 to n
.cells(j,k+7) = average - .cells(j,k)
Next k
Next j
tung:
Sub problem6()
Dim i As Integer, j As Integer
With Range("b2")
For i = 1 To 5
For j = 1 To 5
.Cells(i, j + 6) = .Cells(i, j) - avg(16.16)
Next j
Next i
End With
End Sub

Full codes
Sub count()
With Range("b2")
nrows = Range(.Offset(0, 0), .End(xlDown)).Rows.count
ncols = Range(.Offset(0, 0), .End(xlToRight)).Columns.count
Total = 0
c=0
d=0
n = nrows * ncols
For j = 1 To ncols
For i = 1 To nrows
Total = Total + .Cells(i, j)
Next i
.Cells(i, j) = Total
c = c + Total
Total = 0
Next j
For i = 1 To nrows
For j = 1 To ncols
total1 = total1 + .Cells(i, j)
Next j
.Cells(i, j) = total1
d = d + Total
total1 = 0
Next i
.Cells(i, j) = c + d
avg = (c + d) / n
For i = 1 To nrows
For j = 1 To ncols
.Cells(i, j + 6) = .Cells(i, j) - avg
Next j
Next i
End With
End Sub
Part IV 0 points
Do all the questions
1. Have you taken a deep breath?
2. Let it out?
2010 EXAM

## Part I: Closed book. 20 points

1. Assume that your look up value is in B2 and your range is F1:I22
Provide the EXCEL syntax for using VLOOKUP to return the value in the 3rd column

## =VLOOKUP(B2,F1:I22, 3, FALSE) -- I Think :\

2. You want to test whether cell D2 is greater than 100 if it is set the current cell to the value of cell E2 times 0.9
otherwise set it equal to the value of cell E2 times 1.05
Provide the syntax to do this in Excel using an IF statement
=IF(D2>100,E2*90%,E2*105%)
3. Provide the formula (summation form) for Variance
Sample
N

S^2=

i=1

Population

X
Xi

i=1

( Xi) 2 /n

4. Assume values are stored in range B1:Z1 provide the EXCEL syntax for calculating the standard deviation
STD (B1:Z1) OR STDEVA(B1:Z1)
5. Using an EXCEL formula for generate a random number between 1 and 52
=Randbetween(1,52)
6. Provide the VBA syntax for the IF Then Else statement using an example
IF X > 6 Then
Y=3
Else
Y=2
End If
7. Assume Dim Names (5 to 100) as String in VBA. How do you refer to the i+10th element in Names
Names(i+10)
8. Provide an example of VBA syntax for a Do while loop: ____________________
x=1
Do while x<5
msgbox Hi
x=x+1
Loop
9. Provide an example of the VBA syntax for two nested For Next loops
For i = 1 to 2
For j= 1 to 2
Msgbox(Hi)
Next j
Next i
10. Provide an example of the VBA syntax for a function named Check that takes three values (all integer) and
returns the value that is between the other two. You dont need the code to implement the function just to define it.
Function Check (A as integer, B as integer, C as integer)
If A > B Then
If B> C Then
MsgBox B & is the value that is between the other two
End If
If B > A Then
If A > C Then

## MsgBox A & is the value that is between the other two

End If
If A > C Then
If C > B Then
MsgBox C & is the value that is between the other two
End If
Check = A
End Function
Part II: Open book. No Computer or Calculator. 40 Points
NOTE: YOU Wont be able to use the computer for this section.
Understanding code: Select a total of five questions. As directed.
Assume the following data values on the spreadsheet

OrderID

ProductID

UnitPrice

Quantity

Discount

QOH

Difference

10248

11

14.00

12

42

10248

42

9.80

10

10248

72

34.80

59

10249

14

18.60

38

10249

51

42.40

40

48

10250

41

7.70

10

62

10250

51

42.40

35

0.15

10

10250

65

16.80

15

0.15

10

10251

22

16.80

0.05

18

11

10251

57

15.60

15

0.05

59

12

10251

65

16.80

20

29

13

10252

20

64.80

40

0.05

76

14

10252

33

2.00

25

0.05

48

15

10252

60

27.20

40

88

16

10253

31

10.00

20

14

17

10253

39

14.40

42

60

18

10253

49

16.00

40

19

10254

24

3.60

15

0.15

72

20

10254

55

19.20

21

0.15

26

21

10254

74

8.00

21

54

22

10255

15.20

20

45

23

10255

16

13.90

35

49

24

10255

36

15.20

25

63

25

10255

59

44.00

30

77

26

10256

53

26.20

15

54

27

10256

77

10.40

12

28

10257

27

35.10

25

23

29

10257

39

14.40

12

30

10257

77

10.40

15

13

31

10258

15.20

50

0.2

23

32

10258

17.00

65

0.2

33

10258

32

25.60

0.2

10

34

10259

21

8.00

10

18

35

10259

37

20.80

62

36

10260

41

7.70

16

0.25

32

37

10260

57

15.60

50

83

38

10260

62

39.40

15

0.25

62

39

10260

70

12.00

21

0.25

23

Given the following code determine is the outcome or action? Write your answer in the space provided You will get
five questions total.
Do 1 or 2 Assume D = Range(D2).value, A = Range(D32).Value
1.
B = 10
C=D
F = (A-D)/2
If A-D >= C + F Then

## 2. Using the data above (table)

Count = 0
n=8
F = (A-D)/2
For j = 1 to n

C= D+5
Else
B=B + D
End if
Value of C? 17 Value of B? 10

D = Range(D2).Cells(j, 1)
If D > F then
Count = Count +1
Total = D + Total
Else
Total = Total + D* 2
End If
Next
Value of Count is: 2
Value of Total is: 197

1. D=Range(D2).value
D= 12
A=Range(D32).value
A=65
B=10
C=D; C=12

J=1
D=12
12<26.5
Count = 0
Total = 24

F=(A-D)/2
F=(65-12)/2=26.5
IF A-D >=C+F Then
C=D+5
Else
B=B+D
End if
IF 65-12 >= 12+26.5 Then
C=12+17
C=17
Else B=B+D; B=10
DO: 3. Use data table above
With Range(D2)
For i = 1 to 5
.cells(i, 4) = 0
If .cells(i, 1) < .cells(i,3) Then
.cells(i,4) = .cells(i, 3)-.cells(i,1)
ElseIf .cells(i, 1) > .cells(i, 3) Then
.cells(i,4) = .cells(i,3) - .cells(i, 1)
End if
Next
End With
Output
i=1
.cells(1,1) < .cells(1,3); 12<42
.cells(1,4)=.cells(1,3)-.cells(1,1); 30
i=2
.cells(2,1)>.cells(2,3); 10>5
.cells(2,4)=.cells(2,3)-.cells(2,1);-5
i=3
.cells(3,1)<.cells(3,3); 5<59

J=2
D=10
Count = 0
Total=24+20; 44
J=3
D=5
Count = 0
Total = 44+10; 54
J=4
D=9
Count = 0
Total = 54+18; 72
J=5
D=40
Count=0+1; 1
Total = 40+72; 112
J=6
D=10
Count = 1
Total=112+20; 132
J=7
D=35
Count=1+1; 2
Total = 132+37; 167
J=8
D=15
Total = 167+30; 197

.cells(3,4)=.cells(4,3)-.cells(4,1);54
i=4
.cells(4,1)<.cells(4,3);9<38
.cells(4,4)=.cells(4,3)-.cells(4,1);29
i=5
.cells(5,1)<.cells(5,1);40<48
.cells(5,1)=.cells(5,3)-.cells(5,1);8

## Do 4 or 5 Assume: Dim QOH(1 to 10) as integer, Same spreadsheet data

4.
For k = 1 to 10
QOH(k) = Range(F2).Cells(k,1)
Next
Values of QOH array =
4. QOH(1)=42
QOH(2)=5
QOH(3)=59
QOH(4)=38
QOH(5)=48
QOH(6)=62
QOH(7)=10
QOH(8)=9
QOH(9)=18
QOH(10)=59

## Do 6. Assume original data above

The following produces what?
Total = 0
N=5
LIM = 11
With Range("C2")
For I = 1 To N
If .cells(I, 2) < LIM then
Total = Total + (.cells(I,2) +.cells(I,3))/2
else
Total = Total + .Cells(I, 2)*1.1
End if
Next
End With
Avg = Total / N
What is the value for Avg? 13.84
For Total? 69.2
I=1
.cells(1,2)= 12; 12>11
Total= 0 + 13.2

5.
Total = 0
With Range(D2)
For j = 1 to 4
Range(H2).cells(j,1) = (.cells(j, 1) - .cells(j,2))
Next
End with
Value of Table
J=1
Range(H2).cells(1,1)=.cells(1,1)-.cells(1,2)
=12-0 ; 12
J=2
Range(H2).cells(2,1)=.cells(2,1)-.cells(2,2)
=10-0 ; 10
J=3
Range(H2).cells(3,1)=.cells(3,1)-.cells(3,2)
=5-0 ; 5
J=4
Range(H2).cells(4,1)=.cells(4,1)-.cells(4,2)
=9-0 ; 9

I=2
.cells(2,2)=10 ; 10<11
Total=13.2+5 ; 18.2
I=3
.cells(3,2)=5; 5<11
Total=18.2+2.5 ; 20.7
I=4
.cells(4,2)=9 ; 9<11
Total=20.7+4.5; 25.2
I=5
.cells(5,2)=40 ; 40>11
Total=25.2 + 44 ; 69.2
Avg = 69.2 / 5 ; 13.84

## Do 7 Assume Dim OrdNum(1 to 10) as integer, Products(1 to 10) as integer

I had to change the range of the array of OrdNum and Products from 1 to 6 to 1 to 10 otherwise the code will not
run.
Using the data above
With Range("A2")
Avg = 60
For i = 1 To 10
OrdNum(i) = .cells(i, 1)
Products(i) = .cells(i, 2)
Next
For i = 1 To 10
If Products(i) > Avg then
Msg= Order & OrdNum(i ) & has product numbered & Products(i)
Msgbox Msg & that is greater than 10
End if
Next
End With
What is the output?
i=1
OrdNum(1)=10248
Products(1)=11
i=2
OrdNum(2)=10248
Products(2)=42
i=3
OrdNum(3)=10248
Products(3)=72
i=4
OrdNum(4)=10249
Products(4)=14
i=5
OrdNum(5)=10249
Products(5)=51
i=6
OrdNum(6)=10250
Products(6)=41

i=7
OrdNum(7)=10250
Products(7)=51
i=8
OrdNum(8)=10250
Products(8)=65
i=9
OrdNum(9)=10251
Products(9)=22
i=10
OrdNum(10)=10251
Products(10)=57
Output:
Order 10248 has product numbered 72 that is greater than 10
Order 10250 has product numbered 65 that is greater than 10

## Part III 40 points (1-2, 5 points, 3-6 10 points, 7 is 15 points)

This part is open book, notes, computer. Select four questions out of seven as indicated below.
ASSUME OPTION EXPLICIT AND OPTION BASE 1
DO 1 or 2
1. Write one function that adds the cube of three integer numbers. It returns an Integer Number. Be complete in
your code. Include the Function statements
Option Explicit
Option Base 1
Function xyz(x As Integer, y As Integer, z As Integer) As Integer
xyz = ((x) ^ 3 + (y) ^ 3 + (z) ^ 3)
End Function
Sub callfuction()
MsgBox xyz(2, 3, 5)
End Sub
2. Write a function that when given a stock price it is randomly adjusted up or down by a percentage based on the
given standard deviation. For example
AdjPrice(75.87, 5.75) is an example call that provides the stock price and the standard deviation. This is adjusted by
a random percentage (0 to 1) of the standard deviation value. It can be added or subtracted. So if we got a random
value of 0.9 then the answer could be: 70.70=75.87-0.9*5.75
Remember it is random whether it adjusts the stock price up or down. Down in this example
Function adjprice(STPrice As Single, StdValue As Single) as Single
Dim random As Single
random = Int(rnd * 100) + 1
adjprice = STPrice - ((1 / random) * StdValue)
End Function

Sub call1()
End Sub
End Function
DO EITHER 3 or 4
3. Write a Sub that displays in message boxes the following sequence of statements using a loop.
We have 1 so we have 0 pair and 1 person out
We have 2 so we have 1 pairs
We have 3 so we have 1 pair and 1 person out
We have 4 so we have 2 pairs
We have 5 so we have 2 pairs and 1 person out
Dim i As Single, peep As Integer
Dim pair As String
For i = 1 To 5
If i \ 2 = 1 Or i \ 2 = 0 Then
pair = "pair"
Else
pair = "pairs"
End If
If (i \ 2) * 2 <> i Then
MsgBox ("We have " & i & " so we have " & i \ 2 & " " & pair & " and " & " 1 person out")
Else
MsgBox ("We have " & i & " so we have " & i \ 2 & " " & pair)
End If
Next i
4. Write a Sub the displays the following to the user:
The string has 5 As: ABABABABA
The string has 3 Bs : ABABABA
The string has 3 As: ABABAB
The string has 2 Bs: ABABA
The string has 2 As: ABAB
The string has 1 Bs: ABA
The string has 1 As: AB
The string has 0 Bs: A
Hint: Concatenation: astring = astring & some text
Hint: You can use two MsgBoxes to do this or an if statement and a loop.
Not Sure whether it is right or not
Dim n As Integer, strv As String
n = InputBox("number of times to generate the string")
strv = AB
leng = Len(strv)
strVal = strv
For i = 1 To n
MsgBox strVal
'total = total + Val
strVal = strVal + strv
Next i
n = Len(strVal)
m=n
For i = 1 To n / leng

MsgBox Left(strVal, m)
m = m - leng
Next i
Sub part4P4()
Dim strv As String, letter As String, i As Integer, numA As Integer, numB As Integer
Dim AorB As Integer, num As Integer
strv = "ABABABABA"
AorB = 1
MsgBox ("This string has 5 A's: " & strv)
For i = Len(strv) - 2 To 1 Step -1
strv = Mid(strv, 1, i)
numA = 0
numB = 0
For j = 1 To Len(strv)
If Mid(strv, j, 1) = "A" Then
numA = numA + 1
Else
numB = numB + 1
End If
Next j
If AorB = 0 Then
num = numA
AorB = 1
letter = "A"
Else
num = numB
AorB = 0
letter = "B"
End If
MsgBox ("The string has " & num & " " & letter & "'s: " & strv)
Next i
End Sub
Do 5 or 6
5. A Write a sub that computes the total and averages for each row and column of a table that starts in A1 on
worksheet Operations. We want to make sure we always use the Operations regardless of which sheet we are on.
Assume five rows and columns.
With Range (A2)
n=5
For k = 1 to n
total = 0
for j to 1 to n
Total = Total + .cells (j,k)
Avg = total / n
next j
.cells (n+1,k ) = total
gtotal = gtotal +total
Next k
For j = 1 to n
total = 0
for k = 1 to n
total = total + cells (j,k)

next k
.cells (j,n+1) = total
next j
.cells (n+1,n+1) = gtotal
B. Write another sub that computes the overall average for the data in A and generates another table that is the
difference between each cell in and the average.
6. A. Write a Sub that uses one For Next loop to gets the stock price and year data from the spreadsheet and stores
them in two Arrays: stockValues, Year. There are ten years. Data starts at location D2 with year in column D and
stock prices in column E
Once you have the data entered in the arrays, find the highest stock price and MsgBox the year and stock price.
Sub maxprice()
Dim StockVal(1 To 10) As Double
Dim Years(1 To 10) As Long
Dim year As Long, highest As Double
With Range("D1")
For i = 1 To 10
StockVal(i) = .Cells(i, 1)
Years(i) = .Cells(i, 2)
Next
For i = 1 To 10
If highest < StockVal(i) Then
highest = StockVal(i)
year = Years(i)
End If
Next i
MsgBox "The highest stock price is " & highest & " year is " & year
End With
End Sub
B. Write another sub where you assume that you have the data you used above stored in the spreadsheet. Now
when the user enters a year search for it in the spreadsheet and Msgbox the year and stock price.
HINT: You can use the code for finding the highest but use a different variable name for the largest such as SrchTerm
which is set by the user with an InputBox.
Sub main()
Dim searchyear As Integer, nrows As Integer
searchyear = InputBox("Enter a year")
Call search1(searchyear, nrows)
End Sub
Sub search1(searchyear As Integer, nrows As Integer)
'Dim found As Integer, nrows As Integer
With Range("D2")

## nrows = Range(.Offset(0, 0), .End(xlDown)).Rows.count

found = 0
For i = 1 To nrows
If .Cells(i, 2) = searchyear Then
MsgBox "Stock Price is " & .Cells(i, 1).Value & " at year " & .Cells(i, 2).Value
found = 1
End If
Next i
If found = 0 Then
MsgBox "year is not in the list"
End If
End With
End Sub
Sub part4P_6B()
Dim year As Integer, stockPrice As Double
With Range("D1")
year = InputBox("Enter a year")
For i = 1 To 10
If .Cells(i, 2) = year Then
stockPrice = .Cells(i, 1)
End If
Next i
MsgBox (year & ": " & stockPrice)
End With
End Sub
DO 7
7. A. Write a sub that computes returns (both normal and lognormal) for the stock getting the data from the
B. Write a sub (you can use the one above) that puts the stock returns into an array and uses a function that
calculates the average of the stock returns array. Post these on the spreadsheet.
C. Write the function that computes the average returns.
Study guide..
TIS 420 Exam Information and Study Guide
The exam will be in two parts: Closed part and Open Part.
Closed Part: Two sections
Part I Section 1: you will need to know some basic definitions:
Summation Formula
for NPV
for Average
for Variance
the syntax for:
how to write a sub

a function
how to reference a given element in an array,
how to write the different types of loops (syntax) for next, do while, do until
format for a VBA If statement then
format for a VBA If statement then else
for generating a random value
setting the range
If statement
format for NPV formula
Format for standard deviation
format for the variance
for generating a random value
Part 1 Section 2: Demonstrate your understanding of code
You will be provided code based on If statements, loops, arrays, and control properties (.cells, with end with, ranges)
and you will need to determine the outcome or result. Cell references can be based on the with, cells statements
Closed Book
Part II: You will need to provide code based on a given description.
This will be based on ifs, control properties, loops, arrays, and subs using with, cell, offset, and array references.
What to know in terms of coding:
Know how to generate a random number
Write functions that calculates and return a value based on a formula.
Write a sub that uses a loop for manipulating a string
Know how to write the code for putting values in sequential cells on the spreadsheet or for getting values from a
sequence of cells on the spreadsheet
Know how to write the code for putting values in sequential cells on the spreadsheet from an array or for getting
values from a sequence of cells on the spreadsheet and putting them into an array.
Know how to write the code for summing values in an array, values on the spreadsheet, or from the user
Know how to write the code for averaging values in an array, values on the spreadsheet, or from the user
Know how to write the code for calculating the standard deviation of values in an array, values on the spreadsheet, or
from the user.
Know how to write the code for passing array values to a function and performing any of the above calculations on
the array: average, sum, standard deviation
Know how to write the code to find the highest or lowest value or to search for a given value in a list of values.