Escolar Documentos
Profissional Documentos
Cultura Documentos
Variance =
)^2 / (n)
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
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
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
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:
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
Cadigan
Brulez
Bedrick
Castleman
Cadigan
Brulez
Chandler
Castleman
Cadigan
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
r.RemoveDuplicates Columns:=Array(1), Header:=xlYes
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
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?
3. Ready to RELAX
2010 EXAM
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
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
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
Spreadsheet value(s)? _____________
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
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
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
Sub call1()
MsgBox adjprice(75.87, 5.75)
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")
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
Excel spreadsheet:
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.