-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVBA-Script
242 lines (180 loc) · 7.98 KB
/
VBA-Script
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
Sub Ticker_Summary()
'
' Macro1 Macro
'
'Checks whether all sheets or a specific sheet are selected
If Sheets("Menu").Range("A5").Value = "All Sheets" Then
firstsheet = 2
lastsheet = ThisWorkbook.Sheets.Count
Else
firstsheet = ThisWorkbook.Sheets(Sheets("Menu").Range("A5").Value).Index
lastsheet = ThisWorkbook.Sheets(Sheets("Menu").Range("A5").Value).Index
End If
'Cycles through selected sheets
For i = firstsheet To lastsheet
Worksheets(i).Select
'Freezes window Panes at B2
Range("B2").Select
ActiveWindow.FreezePanes = True
'Creates Variables
lastrow = Range("A1").End(xlDown).Row
'Creates divider for ticker analysis
If Range("I1").Value = "" Then
Columns("H:H").ColumnWidth = 2.45
Columns("H:H").Interior.Color = 0
End If
'Adds Ticker Analysis Headings
Range("I1").Value = "Ticker"
Range("J1").Value = "Yearly Change"
Range("K1").Value = "Percent Change"
Range("L1").Value = "Total Stock Volume"
Range("I1:L1").Font.Bold = True
Columns("J:L").EntireColumn.AutoFit
'Adds Ticker Data
Range("I2").Value = "=UNIQUE(A2:A" & lastrow & ")"
Cells.Replace What:="@", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
FormulaVersion:=xlReplaceFormula2
lasttickerrow = Range("I2").End(xlDown).Row
Range("I2:I" & lasttickerrow).Value = Range("I2:I" & lasttickerrow).Value
tickerrow = 2
Do Until Range("I" & tickerrow).Value = ""
startpricerow = WorksheetFunction.Match(Range("I" & tickerrow).Value, Columns("A:A"), 0)
startprice = Range("C" & startpricerow).Value
closepricerow = startpricerow + WorksheetFunction.CountIf(Columns("A:A"), Range("I" & tickerrow).Value) - 1
closeprice = Range("F" & closepricerow).Value
'Adds Yearly Change
Range("J" & tickerrow).Value = closeprice - startprice
'Adds Percent Change
Range("K" & tickerrow).Value = closeprice / startprice - 1
'Adds Total Stock Volume
Range("L" & tickerrow).Value = WorksheetFunction.Sum(Range("G" & startpricerow & ":G" & closepricerow))
tickerrow = tickerrow + 1
Loop
'Adds Positive Conditional fomratting to Yearly Change
With Range("J2:J" & lasttickerrow)
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=0"
.FormatConditions(Range("J2:J" & lasttickerrow).FormatConditions.Count).SetFirstPriority
End With
With Range("J2:J" & lasttickerrow).FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
Range("J2:J" & lasttickerrow).FormatConditions(1).StopIfTrue = False
'Adds Negative Conditional fomratting to Yearly Change
With Range("J2:J" & lasttickerrow)
.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=0"
.FormatConditions(Range("J2:J" & lasttickerrow).FormatConditions.Count).SetFirstPriority
End With
With Range("J2:J" & lasttickerrow).FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Range("J2:J" & lasttickerrow).FormatConditions(1).StopIfTrue = False
'Changes Percent Change Number Format
Range("K2:K" & lasttickerrow).NumberFormat = "0.00%"
Next i
Sheets("Menu").Select
MsgBox ("The Ticker Summary Data has now been added to your selected sheet(s)")
End Sub
----------------------------------
----------------------------------
Sub Greatest_Summary()
'
' Macro1 Macro
'
'Checks whether all sheets or a specific sheet are selected
If Sheets("Menu").Range("A5").Value = "All Sheets" Then
firstsheet = 2
lastsheet = ThisWorkbook.Sheets.Count
Else
firstsheet = ThisWorkbook.Sheets(Sheets("Menu").Range("A5").Value).Index
lastsheet = ThisWorkbook.Sheets(Sheets("Menu").Range("A5").Value).Index
End If
'Cycles through selected sheets
For i = firstsheet To lastsheet
Worksheets(i).Select
'Creates Variables
lastrow = Range("A1").End(xlDown).Row
'Creates divider for ticker analysis
If Range("N1").Value = "" Then
Columns("M:M").ColumnWidth = 2.45
Columns("M:M").Interior.Color = 0
End If
'Adds Greatest Summary Headings
Range("N2").Value = "Greatest % Increase"
Range("N3").Value = "Greatest % Decrease"
Range("N4").Value = "Greatest Total Volume"
Range("O1").Value = "Ticker"
Range("P1").Value = "Value"
Range("N2:N4").Font.Bold = True
Range("O1:P1").Font.Bold = True
Columns("N:N").EntireColumn.AutoFit
'Adds Greatest % Increase, Ticker and changes number format
Range("P2").Value = WorksheetFunction.Max(Columns("K:K"))
Range("O2").Value = Range("I" & WorksheetFunction.Match(Range("P2").Value, Columns("K:K"), 0)).Value
Range("P2").NumberFormat = "0.00%"
'Adds Greatest % Decrease, Ticker and changes number format
Range("P3").Value = WorksheetFunction.Min(Columns("K:K"))
Range("O3").Value = Range("I" & WorksheetFunction.Match(Range("P3").Value, Columns("K:K"), 0)).Value
Range("P3").NumberFormat = "0.00%"
'Adds Greatest Total Volume and Ticker
Range("P4").Value = WorksheetFunction.Max(Columns("L:L"))
Columns("P:P").EntireColumn.AutoFit
Range("O4").Value = Range("I" & WorksheetFunction.Match(Range("P4").Value, Columns("L:L"), 0)).Value
Next i
Sheets("Menu").Select
MsgBox ("The Greatest Summary Data has now been added to your selected sheet(s)")
End Sub
Sub Greatest_Summary()
'
' Macro1 Macro
'
'
'Checks whether all sheets or a specific sheet are selected
If Sheets("Menu").Range("A5").Value = "All Sheets" Then
firstsheet = 2
lastsheet = ThisWorkbook.Sheets.Count
Else
firstsheet = ThisWorkbook.Sheets(Sheets("Menu").Range("A5").Value).Index
lastsheet = ThisWorkbook.Sheets(Sheets("Menu").Range("A5").Value).Index
End If
'Cycles through selected sheets
For i = firstsheet To lastsheet
Worksheets(i).Select
'Creates Variables
lastrow = Range("A1").End(xlDown).Row
'Creates divider for ticker analysis
If Range("N1").Value = "" Then
Columns("M:M").ColumnWidth = 2.45
Columns("M:M").Interior.Color = 0
End If
'Adds Greatest Summary Headings
Range("N2").Value = "Greatest % Increase"
Range("N3").Value = "Greatest % Decrease"
Range("N4").Value = "Greatest Total Volume"
Range("O1").Value = "Ticker"
Range("P1").Value = "Value"
Range("N2:N4").Font.Bold = True
Range("O1:P1").Font.Bold = True
Columns("N:N").EntireColumn.AutoFit
'Adds Greatest % Increase, Ticker and changes number format
Range("P2").Value = WorksheetFunction.Max(Columns("K:K"))
Range("O2").Value = Range("I" & WorksheetFunction.Match(Range("P2").Value, Columns("K:K"), 0)).Value
Range("P2").NumberFormat = "0.00%"
'Adds Greatest % Decrease, Ticker and changes number format
Range("P3").Value = WorksheetFunction.Min(Columns("K:K"))
Range("O3").Value = Range("I" & WorksheetFunction.Match(Range("P3").Value, Columns("K:K"), 0)).Value
Range("P3").NumberFormat = "0.00%"
'Adds Greatest Total Volume and Ticker
Range("P4").Value = WorksheetFunction.Max(Columns("L:L"))
Columns("P:P").EntireColumn.AutoFit
Range("O4").Value = Range("I" & WorksheetFunction.Match(Range("P4").Value, Columns("L:L"), 0)).Value
Next i
Sheets("Menu").Select
MsgBox ("The Greatest Summary Data has now been added to your selected sheet(s)")
End Sub