-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.vbs
116 lines (94 loc) · 4.02 KB
/
solution.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Sub stock_analysis():
' Set dimensions
Dim total As Double
Dim i As Long
Dim change As Double
Dim j As Integer
Dim start As Long
Dim rowCount As Long
Dim percentChange As Double
Dim days As Integer
Dim dailyChange As Double
Dim averageChange As Double
' Set title row
Range("I1").Value = "Ticker"
Range("J1").Value = "Yearly Change"
Range("K1").Value = "Percent Change"
Range("L1").Value = "Total Stock Volume"
Range("P1").Value = "Ticker"
Range("Q1").Value = "Value"
Range("O2").Value = "Greatest % Increase"
Range("O3").Value = "Greatest % Decrease"
Range("O4").Value = "Greatest Total Volume"
' Set initial values
j = 0
total = 0
change = 0
start = 2
' get the row number of the last row with data
rowCount = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To rowCount
' If ticker changes then print results
If Cells(i + 1, 1).Value <> Cells(i, 1).Value Then
' Stores results in variables
total = total + Cells(i, 7).Value
' Handle zero total volume
If total = 0 Then
' print the results
Range("I" & 2 + j).Value = Cells(i, 1).Value
Range("J" & 2 + j).Value = 0
Range("K" & 2 + j).Value = "%" & 0
Range("L" & 2 + j).Value = 0
Else
' Find First non zero starting value
If Cells(start, 3) = 0 Then
For find_value = start To i
If Cells(find_value, 3).Value <> 0 Then
start = find_value
Exit For
End If
Next find_value
End If
' Calculate Change
change = (Cells(i, 6) - Cells(start, 3))
percentChange = Round((change / Cells(start, 3) * 100), 2)
' start of the next stock ticker
start = i + 1
' print the results
Range("I" & 2 + j).Value = Cells(i, 1).Value
Range("J" & 2 + j).Value = Round(change, 2)
Range("K" & 2 + j).Value = "%" & percentChange
Range("L" & 2 + j).Value = total
' colors positives green and negatives red
Select Case change
Case Is > 0
Range("J" & 2 + j).Interior.ColorIndex = 4
Case Is < 0
Range("J" & 2 + j).Interior.ColorIndex = 3
Case Else
Range("J" & 2 + j).Interior.ColorIndex = 0
End Select
End If
' reset variables for new stock ticker
total = 0
change = 0
j = j + 1
days = 0
' If ticker is still the same add results
Else
total = total + Cells(i, 7).Value
End If
Next i
' take the max and min and place them in a separate part in the worksheet
Range("Q2") = "%" & WorksheetFunction.Max(Range("K2:K" & rowCount)) * 100
Range("Q3") = "%" & WorksheetFunction.Min(Range("K2:K" & rowCount)) * 100
Range("Q4") = WorksheetFunction.Max(Range("L2:L" & rowCount))
' returns one less because header row not a factor
increase_number = WorksheetFunction.Match(WorksheetFunction.Max(Range("K2:K" & rowCount)), Range("K2:K" & rowCount), 0)
decrease_number = WorksheetFunction.Match(WorksheetFunction.Min(Range("K2:K" & rowCount)), Range("K2:K" & rowCount), 0)
volume_number = WorksheetFunction.Match(WorksheetFunction.Max(Range("L2:L" & rowCount)), Range("L2:L" & rowCount), 0)
' final ticker symbol for total, greatest % of increase and decrease, and average
Range("P2") = Cells(increase_number + 1, 9)
Range("P3") = Cells(decrease_number + 1, 9)
Range("P4") = Cells(volume_number + 1, 9)
End Sub