forked from alekrutkowski/Platform-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Macros_in_Scoreboard macros.xlsm.VBA
570 lines (443 loc) · 16.7 KB
/
Macros_in_Scoreboard macros.xlsm.VBA
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
Sub format_disso1()
Dim WS_Count As Integer
Dim i As Integer, c As Integer
Dim bookmarks As Range
Dim filepath As String
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B20").Value
filepath = Replace(filepath, "dissemination", "output")
MsgBox "Opening: " & filepath & "output.xlsx", vbInformation, "Info"
Workbooks.Open Filename:=filepath & "output.xlsx"
Windows("output.xlsx").Activate
WS_Count = ActiveWorkbook.Worksheets.Count - 1 ' -1 because to avoid touching sheet "Cut_offs II"
For i = 1 To WS_Count
Worksheets(i).Select
ActiveWindow.Zoom = 80
ActiveSheet.Columns("A:A").Delete
ActiveSheet.Columns("A:A").ColumnWidth = 15
Next i
For i = 1 To WS_Count - 1
Worksheets(i).Activate
ActiveSheet.Rows("1:1").Delete
ActiveSheet.Rows("1:1").RowHeight = 75
ActiveSheet.Rows("2:2").RowHeight = 30
If i <> 4 Then
ActiveSheet.Rows("3:3").Delete
End If
'ActiveSheet.Range("B3:DD800").NumberFormat = "0.0"
ActiveSheet.Range("B3:DD800").Select
With Selection
.NumberFormat = "0.0"
.Value = .Value
End With
If i = 3 Then
ActiveSheet.Range("B3:DD800").Select
With Selection
.HorizontalAlignment = xlRight
.Replace "nan", ""
End With
End If
If i = 5 Then
ActiveSheet.Range("B3:DD800").NumberFormat = "0.00"
End If
Columns("A:AW").ColumnWidth = 9.71
Rows("1:2").Select
With Selection
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
End With
Next i
Sheets("Differences").Activate
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Rows("3:3").RowHeight = 26.25
Rows("4:4").Delete
Range("A1") = "Indicator"
Range("A2") = "year"
Range("A3") = "diff"
'adding bookmarks
Workbooks.Open Filename:= _
"U:\04 Data and tools\Reports\scoreboard\bookmarks.csv"
Set bookmarks = Range("D1:E122")
Windows("output.xlsx").Activate
Sheets("Headline").Activate
Rows("2:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A2") = "bookmarks"
c = 2
For i = 1 To 16
Cells(2, c).Select
If c = 5 Then
ActiveCell.Resize(1, 1).Select
Else: ActiveCell.Resize(1, 3).Select
End If
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.Value = Application.VLookup(ActiveCell.Offset(-1, 0).Value, bookmarks, 2, False)
c = c + 3
If c = 8 Then
c = c - 2
End If
Next i
Rows("2:2").RowHeight = 22.2
Workbooks("bookmarks.csv").Close
Windows("output.xlsx").Activate
Worksheets("Cut_offs II").Range("C2:F32").NumberFormat = "0.0"
Worksheets("Cut_offs II").Columns(1).AutoFit
Worksheets("Cut_offs II").Columns(2).AutoFit
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B24").Value
' Turn off warnings to overwrite the file without prompts
Application.DisplayAlerts = False
' Save the active workbook to the specified filepath
ActiveWorkbook.SaveAs Filename:=filepath
ActiveWorkbook.Close
' Turn warnings back on
Application.DisplayAlerts = True
MsgBox "Saved transformed output.xlsx as: " & filepath, vbInformation, "Info"
End Sub
Sub format_disso2()
Dim WS_Count As Integer
Dim i As Integer
Dim ids As Range, cell As Range, indicators As Range
Dim filepath As String
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B20").Value
filepath = Replace(filepath, "dissemination", "output")
MsgBox "Opening: " & filepath & "COLOURS.xlsx", vbInformation, "Info"
Workbooks.Open Filename:=filepath & "COLOURS.xlsx"
Windows("COLOURS.xlsx").Activate
Sheets("All").Select
Range("D2:J17").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Turn off warnings to overwrite the file without prompts
Application.DisplayAlerts = False
' Save the active workbook to the specified filepath
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B25").Value
ActiveWorkbook.SaveAs Filename:=filepath
ActiveWorkbook.Close
' Turn warnings back on
Application.DisplayAlerts = True
MsgBox "Saved COLOURS.xlsx as: " & filepath, vbInformation, "Info"
' Workbooks.Open Filename:= _
' "U:\04 Data and tools\Reports\scoreboard\Headline indicators table.csv"
' Set indicators = Range("A1:B16")
'
' Windows("COLOURS.xlsx").Activate
' WS_Count = ActiveWorkbook.Worksheets.Count
'
' Worksheets(1).Select
'
' ActiveWindow.Zoom = 80
' ActiveSheet.Columns("A:A").Delete
' ActiveSheet.Columns("A:A").ColumnWidth = 25
' ActiveSheet.Columns("B:Q").ColumnWidth = 15
'
' Range("B1:Q1").Select
' With Selection
' .HorizontalAlignment = xlCenter
' .VerticalAlignment = xlCenter
' .WrapText = True
' .Font.Bold = True
' End With
'
' Rows("1:1").RowHeight = 102
' Rows("2:8").EntireRow.AutoFit
'
' Set ids = Range("B1:Q1")
'
' For Each cell In ids
'
' cell.Value = Application.VLookup(cell.Value, indicators, 2, False)
'
' Next cell
'
' Application.DisplayAlerts = False
' For i = WS_Count To 2 Step -1
' Worksheets(i).Delete
' Next i
' Application.DisplayAlerts = True
'
' Windows("Headline indicators table.csv").Close
End Sub
Sub format_disso3()
Dim WS_Count As Integer
Dim i As Integer
Dim sh_name As String
Dim freq As String
Dim rng As Range
Dim cell As Range
Dim filepath As String
'freq = InputBox("Are these quarterly or yearly indicators? Type q for quarterly or y for yearly")
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B20").Value
filepath = Replace(filepath, "dissemination", "output")
MsgBox "Opening: " & filepath & "COLOURS.xlsx", vbInformation, "Info"
Workbooks.Open Filename:=filepath & "COLOURS.xlsx"
MsgBox "Opening: U:\04 Data and tools\Reports\scoreboard\static_scoreboard.xlsx", vbInformation, "Info"
Workbooks.Open Filename:="U:\04 Data and tools\Reports\scoreboard\static_scoreboard.xlsx"
Windows("static_scoreboard.xlsx").Activate
'Windows("static_scoreboard_q.xlsx").Activate
Dim ws As Worksheet
' Loop through each worksheet in reverse order
' Reverse order is used to avoid skipping sheets when deleting
For i = Sheets.Count To 1 Step -1
Set ws = Sheets(i)
' Check if the name of the worksheet starts with "Sheet"
If ws.Name Like "Sheet*" Then
Application.DisplayAlerts = False ' Disable alerts to avoid confirmation dialog
ws.Delete
Application.DisplayAlerts = True ' Enable alerts again
End If
Next i
WS_Count = ActiveWorkbook.Worksheets.Count
For i = 1 To WS_Count
Worksheets(i).Activate
Columns("A:H").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Next i
Workbooks.Open Filename:= _
"U:\04 Data and tools\Reports\scoreboard\Colour template.xlsx"
Range("A1:G14").Select
Selection.Copy
Windows("static_scoreboard.xlsx").Activate
'Windows("static_scoreboard_q.xlsx").Activate
For i = 1 To WS_Count
Worksheets(i).Activate
Range("A1").Select
ActiveSheet.Paste
Next i
Application.CutCopyMode = False
For i = 2 To WS_Count + 1
Windows("COLOURS.xlsx").Activate
Worksheets(i).Activate
sh_name = ActiveSheet.Name
Range("C3:G7").Copy
Windows("static_scoreboard.xlsx").Activate
'Windows("static_scoreboard_q.xlsx").Activate
Sheets(sh_name).Select
Range("C10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next i
Application.CutCopyMode = False
For i = 1 To WS_Count
Worksheets(i).Activate
Columns("B:G").ColumnWidth = 24
Set rng = ActiveSheet.Range("C10:G14")
For Each cell In rng
cell.EntireRow.AutoFit
Next cell
Next i
Workbooks("Colour template.xlsx").Close SaveChanges:=False
Windows("static_scoreboard.xlsx").Activate
Sheets("ID22").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.Axes(xlCategory).MinimumScale = 60
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.Axes(xlCategory).MinimumScale = 60
Sheets("ID1").Select
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B26").Value
' Turn off warnings to overwrite the file without prompts
Application.DisplayAlerts = False
' Save the active workbook to the specified filepath
ActiveWorkbook.SaveAs Filename:=filepath
ActiveWorkbook.Close
' Turn warnings back on
Application.DisplayAlerts = True
Workbooks("COLOURS.xlsx").Close SaveChanges:=False
MsgBox "Saved transformed static_scoreboard.xlsx as: " & filepath, vbInformation, "Info"
End Sub
Sub format_disso4()
Dim data As Range
Dim col_count As Integer
Dim i As Integer
Dim WS_Count As Integer
Dim MS As String
Dim r As Integer
Dim currentDate As String
Dim filepath As String
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B20").Value
filepath = Replace(filepath, "dissemination", "output")
MsgBox "Opening: " & filepath & "SCATTER.csv", vbInformation, "Info"
Workbooks.Open Filename:=filepath & "SCATTER.csv"
MsgBox "Opening: U:\04 Data and tools\Reports\scoreboard\Template.xlsx", vbInformation, "Info"
Workbooks.Open Filename:="U:\04 Data and tools\Reports\scoreboard\Template.xlsx"
currentDate = Format(Date, "dd mmmm yyyy")
Windows("SCATTER.csv").Activate
col_count = Range("A1", ActiveSheet.Range("a1").End(xlDown)).Count
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B1").Value = "ind_num"
For i = 1 + 1 To col_count
Cells(i, 2).Value = Mid(Cells(i, 1), 3, 3)
Next i
Range("A1").CurrentRegion.Name = "data"
Range("data").Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlYes, Key2:=Range("B1"), Order2:=xlAscending
Windows("Template.xlsx").Activate
WS_Count = ActiveWorkbook.Worksheets.Count
For i = 1 To WS_Count
Worksheets(i).Select
MS = ActiveSheet.Name
Windows("SCATTER.csv").Activate
Range("data").Find(MS, MatchCase:=True).Select
ActiveCell.Offset(0, 9).Select
r = ActiveCell.Row
Range("M2:M17").Offset(r - 2, 0).Replace "", "N/A", xlWhole
Range("M2:M17").Offset(r - 2, 0).Copy Workbooks("Template.xlsx").Worksheets(MS).Range("R2:R17")
Range("F2:F17").Offset(r - 2, 0).Copy Workbooks("Template.xlsx").Worksheets(MS).Range("I2:I17")
Windows("Template.xlsx").Activate
For Each c In Range("C2:C17")
c.Interior.Color = c.DisplayFormat.Interior.Color
c.Font.Color = c.DisplayFormat.Font.Color
c.Interior.Pattern = c.DisplayFormat.Interior.Pattern
Next c
For Each c In Range("I2:I17")
c.Interior.Color = c.DisplayFormat.Interior.Color
c.Font.Color = c.DisplayFormat.Font.Color
c.Interior.Pattern = c.DisplayFormat.Interior.Pattern
Next c
Range("C2:I17").FormatConditions.Delete
Range("R2:R17").Delete
Range("A19").Value = Replace(Range("A19").Value, "DD MMMM YYYY", currentDate)
Range("I2:I17").Borders(xlInsideHorizontal).LineStyle = xlContinuous
Range("I2:I17").Borders(xlInsideHorizontal).Weight = xlThin
Range("I1:I17").Borders(xlEdgeRight).LineStyle = xlContinuous
Range("I1:I17").Borders(xlEdgeRight).Weight = xlThin
Next i
Windows("SCATTER.csv").Activate
DeleteColumnByTitle ("ind_num") ' clean-up
Windows("Template.xlsx").Activate
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B27").Value
' Turn off warnings to overwrite the file without prompts
Application.DisplayAlerts = False
' Save the active workbook to the specified filepath
ActiveWorkbook.SaveAs Filename:=filepath
ActiveWorkbook.Close
Workbooks("SCATTER.csv").Close SaveChanges:=False
' Turn warnings back on
Application.DisplayAlerts = True
MsgBox "Saved transformed Template.xlsx as: " & filepath, vbInformation, "Info"
End Sub
Sub format_disso5()
Dim filepath As String
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B20").Value
filepath = Replace(filepath, "dissemination", "output")
MsgBox "Opening: " & filepath & "For_SCF_tables_Input_Data_worksheet.csv", vbInformation, "Info"
Workbooks.Open Filename:=filepath & "For_SCF_tables_Input_Data_worksheet.csv"
MsgBox "Opening: U:\04 Data and tools\Reports\scoreboard\JER SCF Tables.xlsx", vbInformation, "Info"
Workbooks.Open Filename:="U:\04 Data and tools\Reports\scoreboard\JER SCF Tables.xlsx"
Windows("For_SCF_tables_Input_Data_worksheet.csv").Activate
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("JER SCF Tables.xlsx").Activate
Sheets("NEW For_SCF_tables_Input_Data_w").Select
ActiveSheet.Paste
filepath = Workbooks("Scoreboard macros.xlsm").Sheets(1).Range("B28").Value
' Turn off warnings to overwrite the file without prompts
Application.DisplayAlerts = False
' Save the active workbook to the specified filepath
ActiveWorkbook.SaveAs Filename:=filepath
ActiveWorkbook.Close
' Turn warnings back on
Application.DisplayAlerts = True
Workbooks("For_SCF_tables_Input_Data_worksheet.csv").Close SaveChanges:=False
MsgBox "Saved updated `JER SCF Tables.xlsx` as: " & filepath, vbInformation, "Info"
End Sub
Sub fillpattern()
'
' fillpattern Macro
'
'
ActiveWindow.ActivateNext
Range("C10:H10").Select
With Selection.Interior
.Pattern = xlLightUp
.PatternColorIndex = xlAutomatic
.Color = 14277081
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
Sub Macro2()
'
' Macro2 Macro
'
'
Range("E11").Select
ActiveWindow.ActivateNext
Range("C10:H10").Select
With Selection.Interior
.Pattern = xlLightDown
.PatternColorIndex = xlAutomatic
.Color = 14277081
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
Sub DeleteColumnByTitle(colTitle As String)
Dim ws As Worksheet
Dim col As Range
Dim lastCol As Long
' Set the active worksheet to a variable
Set ws = ActiveSheet
' Find the last used column in the first row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
' Loop through each cell in the first row to find the column with the given title
For Each col In ws.Range(ws.Cells(1, 1), ws.Cells(1, lastCol))
If col.Value = colTitle Then
col.EntireColumn.Delete
Exit For
End If
Next col
End Sub
Sub SaveCellsToTextFile()
Dim fso As Object
Dim txtFile As Object
Dim cellContent As String
Dim filepath As String
Dim i As Integer
' Create a File System Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Define the path of the text file
filepath = Environ("USERPROFILE") & "\File names to be used.txt"
' Create a text file (if it doesn't exist, it will be created; if it exists, it will be overwritten)
Set txtFile = fso.CreateTextFile(filepath, True)
' Loop through cells A1 to A4 to read their content and write to the text file
For i = 24 To 27
cellContent = Worksheets("Sheet1").Range("B" & i).Value
txtFile.WriteLine (cellContent)
Next i
' Close the text file
txtFile.Close
' Open the text file in Notepad
Shell "notepad.exe " & filepath, vbNormalFocus
End Sub
Sub testing()
' Placeholder macro for testing new code snippets
Range("I1:I17").Borders(xlEdgeRight).LineStyle = xlContinuous
Range("I1:I17").Borders(xlEdgeRight).Weight = xlThin
End Sub