-
Notifications
You must be signed in to change notification settings - Fork 3
/
Assembler.bas
532 lines (473 loc) · 16.7 KB
/
Assembler.bas
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
Attribute VB_Name = "Assembler"
Option Explicit
'Instructions
'1. Create an Excel file called Assembler.xlsm (for example) in the same folder as Installer.bas:
' *\Excel-Pomodoro-Timer\
'2. Open the VB Editor (Alt+F11) right click on the Installer VB Project and choose Import a file and chose:
' *\Excel-Pomodoro-Timer\Assembler.bas
'3. Run Assemble from the module Assembler (Click somewhere inside the macro and press F5).
' Make sure to wait for the confirmation message at the end before doing anything with Excel.
'4. Use the tool vbaDeveloper (Available here: https://github.com/DecimalTurn/vbaDeveloper/releases) to import the VBA code.
' - Open vbaDeveloper.xlam
' - Look at the Add-ins ribbon and choose: vbaDeveloper > Import code for ... > Pomodoro_Timer.xlsb
'5. Save the file
Public Const SHORT_NAME = "Pomodoro_Timer"
Public Const EXT = ".xlsb"
Sub Assemble()
If testFileLocation = False Then
Exit Sub
End If
On Error Resume Next
Workbooks(SHORT_NAME & EXT).Close SaveChanges:=False
On Error GoTo 0
Dim wb As Workbook
Set wb = Workbooks.Add
Dim sht As Worksheet
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Dim sht4 As Worksheet
Dim tmpRng As Range
Set sht1 = wb.Sheets.Add
sht1.Name = "Pomodoro"
'Delete any other sheet
For Each sht In wb.Sheets
If sht.Name <> sht1.Name Then
Application.DisplayAlerts = False
sht.Delete
Application.DisplayAlerts = True
End If
Next
'Create the remaining sheets
Set sht2 = wb.Sheets.Add(After:=Sheets(Sheets.Count))
sht2.Name = "Summary"
Set sht3 = wb.Sheets.Add(After:=Sheets(Sheets.Count))
sht3.Name = "Recent"
Set sht4 = wb.Sheets.Add(After:=Sheets(Sheets.Count))
sht4.Name = "Settings"
Range("A1").Select
'*******************************
'Sheet Pomodoro
'*******************************
Set sht = sht1
sht.Select
'Column width
sht.Columns(1).ColumnWidth = 61.2 / 5
sht.Columns(2).ColumnWidth = 83.4 / 5
sht.Columns(3).ColumnWidth = 83.4 / 5
sht.Columns(4).ColumnWidth = 70.8 / 5
sht.Columns(5).ColumnWidth = 215.4 / 5
sht.Columns(6).ColumnWidth = 150.6 / 5
'Cells values
sht.Cells(2, 4).Value2 = "Task Name:"
sht.Cells(8, 1).Value2 = "Date"
sht.Cells(8, 2).Value2 = "Start"
sht.Cells(8, 3).Value2 = "End"
sht.Cells(8, 4).Value2 = "Completed"
sht.Cells(8, 5).Value2 = "Task"
sht.Cells(8, 6).Value2 = "Comment"
sht.Cells(9, 1).Value2 = DateSerial(2017, 11, 25)
sht.Cells(10, 1).Value2 = DateSerial(2017, 11, 25)
sht.Cells(11, 1).Value2 = DateSerial(2017, 11, 25)
sht.Cells(9, 2).Value2 = 43064.60846
sht.Cells(10, 2).Value2 = 43064.69906
sht.Cells(11, 2).Value2 = 43064.72807
sht.Cells(9, 3).Value2 = 43064.62582
sht.Cells(10, 3).Value2 = 43064.71642
sht.Cells(11, 3).Value2 = 43064.74543
sht.Cells(9, 4).Value2 = True
sht.Cells(10, 4).Value2 = True
sht.Cells(11, 4).Value2 = True
sht.Cells(9, 5).Value2 = "Check emails"
sht.Cells(10, 5).Value2 = "Make phone call"
sht.Cells(11, 5).Value2 = "Reading"
'Bulk formatting
sht.Range(Cells(9, 1), Cells(9, 1).End(xlDown).End(xlDown)).NumberFormat = "YYYY-MM-DD"
sht.Range(Cells(9, 2), Cells(9, 3).End(xlDown).End(xlDown)).NumberFormat = "HH:MM AM/PM"
'Cell borders
Dim x As Variant
With sht.Cells(2, 5)
For Each x In Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight)
With .Borders(x)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Next x
End With
'Data validation
Range("E2").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Recent!$A$2:$A$10"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = False
End With
'Table
sht.ListObjects.Add(xlSrcRange, sht.Range("$A$8:$F$200"), , xlYes).Name = "Table24"
'Buttons
Dim bt1 As Button
Set tmpRng = Range("A2:B3")
Set bt1 = sht.Buttons.Add(Left:=tmpRng.Left, Top:=tmpRng.Top, Width:=tmpRng.Width, Height:=tmpRng.Height)
bt1.OnAction = "Pomodoro_Timer.xlsb!PomodoroSession"
bt1.Text = "Start"
Dim bt2 As Button
Set tmpRng = Range("A5:B6")
Set bt2 = sht.Buttons.Add(Left:=tmpRng.Left, Top:=tmpRng.Top, Width:=tmpRng.Width, Height:=tmpRng.Height)
bt2.OnAction = "Pomodoro_Timer.xlsb!Clear_all_records"
bt2.Text = "Clear Records"
Range("A1").Select
'*******************************
'Summary Sheet
'*******************************
Set sht = sht2
sht.Select
'Column width
sht.Columns(1).ColumnWidth = 131.4
sht.Columns(2).ColumnWidth = 87
'Pivot Table
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table24", Version:=6).CreatePivotTable TableDestination:="'" & sht.Name & "'!R1C1", _
TableName:="PivotTable1", DefaultVersion:=6
With sht.PivotTables("PivotTable1").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
sht.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
'Filter
With sht.PivotTables("PivotTable1").PivotFields("Date")
.Orientation = xlPageField
.Position = 1
End With
'Row values
With sht.PivotTables("PivotTable1").PivotFields("Task")
.Orientation = xlRowField
.Position = 1
End With
'Calculated fields
sht.PivotTables("PivotTable1").CalculatedFields.Add "Duration", _
"=End - Start", True
sht.PivotTables("PivotTable1").AddDataField sht.PivotTables( _
"PivotTable1").PivotFields("Duration"), "Duration ", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Duration ")
.NumberFormat = "hh:mm;@"
End With
'Buttons
Dim btx As Button
Set btx = sht.Buttons.Add(270, 0.6, 191.4, 28.2)
btx.OnAction = "Pomodoro_Timer.xlsb!Refresh_Summary_PivotTable"
btx.Text = "Refresh Table"
Range("A1").Select
'*******************************
'Recent Sheet
'*******************************
Set sht = sht3
sht.Select
'Column width
sht.Columns(1).ColumnWidth = 140 / 5
'Cell values
sht.Cells(1, 1).Value2 = "Recent Tasks"
sht.Cells(2, 1).Value2 = "Check emails"
sht.Cells(3, 1).Value2 = "Make phone call"
sht.Cells(4, 1).Value2 = "Reading"
'Formatting
With sht.Cells(1, 1).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 12874308
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With sht.Cells(1, 1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
sht.Cells(1, 1).Font.Bold = True
'Buttons
Dim bt3 As Button
Set bt3 = sht.Buttons.Add(270, 0.6, 191.4, 28.2)
bt3.OnAction = "Pomodoro_Timer.xlsb!Clear_Recent_Tasks"
bt3.Text = "Clear Recent Task"
Range("A1").Select
'*******************************
'Settings Sheet
'*******************************
Set sht = sht4
sht.Select
'Column width
sht.Columns(1).ColumnWidth = 200 / 5
sht.Columns(2).ColumnWidth = 55 / 5
'Cell values
sht.Cells(1, 1).Value2 = "Settings"
sht.Cells(2, 1).Value2 = "Pomodoro duration (min)"
sht.Cells(3, 1).Value2 = "Pomodoro duration (sec)"
sht.Cells(4, 1).Value2 = "Break duration (min)"
sht.Cells(5, 1).Value2 = "Break duration (sec)"
sht.Cells(6, 1).Value2 = "Open Timer in a separate Excel instance"
sht.Cells(7, 1).Value2 = "Reactivate Excel window when timer is closed"
sht.Cells(8, 1).Value2 = "Record unfinished Pomodoro session"
sht.Cells(9, 1).Value2 = "Don't record if session was less than (min)"
sht.Cells(10, 1).Value2 = "Play sound at the end of Pomodoro session"
sht.Cells(11, 1).Value2 = "Play sound at the end of Break"
sht.Cells(12, 1).Value2 = "Use custom position"
sht.Cells(13, 1).Value2 = "Left position"
sht.Cells(14, 1).Value2 = "Top position"
sht.Cells(15, 1).Value2 = "Use shortcuts (F10)"
sht.Cells(16, 1).Value2 = "Flasing color"
sht.Cells(1, 2).Value2 = "Value"
sht.Cells(2, 2).Value2 = 25
sht.Cells(3, 2).Value2 = 0
sht.Cells(4, 2).Value2 = 5
sht.Cells(5, 2).Value2 = 0
sht.Cells(6, 2).Value2 = True
sht.Cells(7, 2).Value2 = True
sht.Cells(8, 2).Value2 = True
sht.Cells(9, 2).Value2 = 1
sht.Cells(10, 2).Value2 = True
sht.Cells(11, 2).Value2 = True
sht.Cells(12, 2).Value2 = False
sht.Cells(13, 2).Value2 = 0.5
sht.Cells(14, 2).Value2 = 0.5
sht.Cells(15, 2).Value2 = True
sht.Cells(16, 2).Value2 = ""
'Table
sht.ListObjects.Add(xlSrcRange, sht.Range("$A$1:$B$16"), , xlYes).Name = "Table2"
'Formatting
With sht.Range("B16").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 16711680
.TintAndShade = 0
.PatternTintAndShade = 0
End With
sht.Range("B13:B14").Style = "Percent"
'Data validation
sht.Select
Range("B2").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="0", Formula2:="=24*60"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B3").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="0", Formula2:="60"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B4").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="0", Formula2:="=24*60"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B5").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="0", Formula2:="60"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B6").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="TRUE,FALSE"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B7").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="TRUE,FALSE"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B8").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="TRUE,FALSE"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B9").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="1", Formula2:="=B2"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B10").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="TRUE,FALSE"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B11").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="TRUE,FALSE"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B12").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="TRUE,FALSE"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B13").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween, Formula1:="0", Formula2:="100"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B14").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween, Formula1:="0", Formula2:="100"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("B15").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="TRUE,FALSE"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Range("A1").Select
Sheets("Pomodoro").Select
wb.SaveAs Filename:=ThisWorkbook.Path & "\" & SHORT_NAME & EXT, FileFormat:=xlExcel12
End Sub
Function testFileLocation() As Boolean
Dim ErrMsg As String
'Test if this workbook has been saved
Dim FileEverSaved As Boolean
If ThisWorkbook.Path = "" Then
ErrMsg = "Please save the file that contains the Assembler module in the same folder than Installer.bas and try again"
MsgBox ErrMsg, vbCritical
testFileLocation = False
Exit Function
End If
'Test if the src folder contains a folder with the right name
Dim SourceFolderExist As Boolean
If Dir(ThisWorkbook.Path & "\src\" & SHORT_NAME & EXT, vbDirectory) = "" Then
ErrMsg = "Please save the file that contains the Assembler module in a location where the source folder (src) contains a folder named " & SHORT_NAME & EXT
MsgBox ErrMsg, vbCritical
testFileLocation = False
Exit Function
End If
testFileLocation = True
End Function