-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVBA_Challenge.vbs
145 lines (101 loc) · 3.83 KB
/
VBA_Challenge.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Attribute VB_Name = "Module2"
Sub AllStocksAnalysisRefactored()
Dim startTime As Single
Dim endTime As Single
Set ws4 = Sheet4.Cells
yearValue = InputBox("What year would you like to run the analysis on?")
startTime = Timer
'Clear worksheet at the start of subroutine
Call Module1.ClearWorksheet
'Format the output sheet on All Stocks Analysis worksheet
ws4(1, 1).Value = "All Stocks (" + yearValue + ")"
ws4(1, 1).Font.Bold = True
'Activate Data Sheet
If yearValue = 2017 Then
Set ws = Sheet1.Cells
End If
If yearValue = 2018 Then
Set ws = Sheet2.Cells
End If
If yearValue = "" Then
MsgBox ("Please select an year for Analysis")
Exit Sub
End If
'create a header row
ws4(3, 1).Value = "Year"
ws4(3, 2).Value = "Total Daily volume"
ws4(3, 3).Value = "Return"
'Initialize array of all tickers
Dim tickers(12) As String
tickers(0) = "AY"
tickers(1) = "CSIQ"
tickers(2) = "DQ"
tickers(3) = "ENPH"
tickers(4) = "FSLR"
tickers(5) = "HASI"
tickers(6) = "JKS"
tickers(7) = "RUN"
tickers(8) = "SEDG"
tickers(9) = "SPWR"
tickers(10) = "TERP"
tickers(11) = "VSLR"
'Get the number of rows to loop over
RowCount = ws(Rows.Count, "A").End(xlUp).Row
'1a) Create a ticker Index
Dim tickerIndex As Integer
tickerIndex = 0
'1b) Create three output arrays
Dim tickerVolumes(12) As Long
Dim tickerStartingPrices(12) As Single
Dim tickerEndingPrices(12) As Single
''2a) Create a for loop to initialize the tickerVolumes to zero
For i = 0 To 11
tickerVolumes(i) = 0
Next i
''2b) Loop over all the rows in the spreadsheet
For j = 2 To RowCount
'3a) Increase volume for current ticker
If ws(j, 1).Value = tickers(tickerIndex) Then
tickerVolumes(tickerIndex) = tickerVolumes(tickerIndex) + ws(j, 8).Value
End If
'3b) Check if the current row is the first row with the selected tickerIndex
If ws(j - 1, 1).Value <> tickers(tickerIndex) And ws(j, 1).Value = tickers(tickerIndex) Then
tickerStartingPrices(tickerIndex) = ws(j, 6).Value
End If
'3c) check if the current row is the last row with the selected ticker
If ws(j + 1, 1).Value <> tickers(tickerIndex) And ws(j, 1).Value = tickers(tickerIndex) Then
tickerEndingPrices(tickerIndex) = ws(j, 6).Value
'3d) Increase the tickerIndex.
tickerIndex = tickerIndex + 1
End If
Next j
'4) Code to output data for current ticker(i), "Total Daily Volume", and "Return"
For i = 0 To 11
ws4(4 + i, 1).Value = tickers(i)
ws4(4 + i, 2).Value = tickerVolumes(i)
ws4(4 + i, 3).Value = tickerEndingPrices(i) / tickerStartingPrices(i) - 1
Next i
'Formatting
Set fs = Sheet4
fs.Range("A3:C3").Font.FontStyle = "Bold"
fs.Range("A3:C3").Borders(xlEdgeBottom).LineStyle = xlContinuous
fs.Range("B4:B15").NumberFormat = "#,##0"
fs.Range("C4:C15").NumberFormat = "0.0%"
fs.Columns("B").AutoFit
dataRowStart = 4
dataRowEnd = 15
For i = dataRowStart To dataRowEnd
If ws4(i, 3) > 0 Then
'Color the cell green
ws4(i, 3).Interior.Color = vbGreen
ElseIf ws4(i, 3) < 0 Then
'Color the cell red
ws4(i, 3).Interior.Color = vbRed
Else
'Clear the cell color
ws4(i, 3).Interior.Color = xlNone
End If
Next i
endTime = Timer
MsgBox "This code ran in " & (endTime - startTime) & " seconds for the year " & (yearValue)
End Sub