-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge 2 Completed VBA SCRIPT.vb
157 lines (111 loc) · 5.8 KB
/
Challenge 2 Completed VBA SCRIPT.vb
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
Sub MultipleYearStockData():
For Each ws In Worksheets
Dim WorksheetName As String
'Current row
Dim i As Long
'Start row of ticker block
Dim j As Long
'Index counter to fill Ticker row
Dim TickCount As Long
'Last row column A
Dim LastRowA As Long
'last row column I
Dim LastRowI As Long
'Variable for percent change calculation
Dim PerChange As Double
'Variable for greatest increase calculation
Dim GreatIncr As Double
'Variable for greatest decrease calculation
Dim GreatDecr As Double
'Variable for greatest total volume
Dim GreatVol As Double
'Get the WorksheetName
WorksheetName = ws.Name
'Create column headers
ws.Cells(1, 9).Value = "Ticker"
ws.Cells(1, 10).Value = "Yearly Change"
ws.Cells(1, 11).Value = "Percent Change"
ws.Cells(1, 12).Value = "Total Stock Volume"
ws.Cells(1, 16).Value = "Ticker"
ws.Cells(1, 17).Value = "Value"
ws.Cells(2, 15).Value = "Greatest % Increase"
ws.Cells(3, 15).Value = "Greatest % Decrease"
ws.Cells(4, 15).Value = "Greatest Total Volume"
'Set Ticker Counter to first row
TickCount = 2
'Set start row to 2
j = 2
'Find the last non-blank cell in column A
LastRowA = ws.Cells(Rows.Count, 1).End(xlUp).Row
'MsgBox ("Last row in column A is " & LastRowA)
'Loop through all rows
For i = 2 To LastRowA
'Check if ticker name changed
If ws.Cells(i + 1, 1).Value <> ws.Cells(i, 1).Value Then
'Write ticker in column I (#9)
ws.Cells(TickCount, 9).Value = ws.Cells(i, 1).Value
'Calculate and write Yearly Change in column J (#10)
ws.Cells(TickCount, 10).Value = ws.Cells(i, 6).Value - ws.Cells(j, 3).Value
'Conditional formating
If ws.Cells(TickCount, 10).Value < 0 Then
'Set cell background color to red
ws.Cells(TickCount, 10).Interior.ColorIndex = 3
Else
'Set cell background color to green
ws.Cells(TickCount, 10).Interior.ColorIndex = 4
End If
'Calculate and write percent change in column K (#11)
If ws.Cells(j, 3).Value <> 0 Then
PerChange = ((ws.Cells(i, 6).Value - ws.Cells(j, 3).Value) / ws.Cells(j, 3).Value)
'Percent formating
ws.Cells(TickCount, 11).Value = Format(PerChange, "Percent")
Else
ws.Cells(TickCount, 11).Value = Format(0, "Percent")
End If
'Calculate and write total volume in column L (#12)
ws.Cells(TickCount, 12).Value = WorksheetFunction.Sum(Range(ws.Cells(j, 7), ws.Cells(i, 7)))
'Increase TickCount by 1
TickCount = TickCount + 1
'Set new start row of the ticker block
j = i + 1
End If
Next i
'Find last non-blank cell in column I
LastRowI = ws.Cells(Rows.Count, 9).End(xlUp).Row
'MsgBox ("Last row in column I is " & LastRowI)
'Prepare for summary
GreatVol = ws.Cells(2, 12).Value
GreatIncr = ws.Cells(2, 11).Value
GreatDecr = ws.Cells(2, 11).Value
'Loop for summary
For i = 2 To LastRowI
'For greatest total volume--check if next value is larger--if yes take over a new value and populate ws.Cells
If ws.Cells(i, 12).Value > GreatVol Then
GreatVol = ws.Cells(i, 12).Value
ws.Cells(4, 16).Value = ws.Cells(i, 9).Value
Else
GreatVol = GreatVol
End If
'For greatest increase--check if next value is larger--if yes take over a new value and populate ws.Cells
If ws.Cells(i, 11).Value > GreatIncr Then
GreatIncr = ws.Cells(i, 11).Value
ws.Cells(2, 16).Value = ws.Cells(i, 9).Value
Else
GreatIncr = GreatIncr
End If
'For greatest decrease--check if next value is smaller--if yes take over a new value and populate ws.Cells
If ws.Cells(i, 11).Value < GreatDecr Then
GreatDecr = ws.Cells(i, 11).Value
ws.Cells(3, 16).Value = ws.Cells(i, 9).Value
Else
GreatDecr = GreatDecr
End If
'Write summary results in ws.Cells
ws.Cells(2, 17).Value = Format(GreatIncr, "Percent")
ws.Cells(3, 17).Value = Format(GreatDecr, "Percent")
ws.Cells(4, 17).Value = Format(GreatVol, "Scientific")
Next i
'Djust column width automatically
Worksheets(WorksheetName).Columns("A:Z").AutoFit
Next ws
End Sub