-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainModule.bas
More file actions
808 lines (651 loc) · 24.6 KB
/
Copy pathMainModule.bas
File metadata and controls
808 lines (651 loc) · 24.6 KB
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
Attribute VB_Name = "MainModule"
' Manufacturing Workflow Scheduler - Main Module
' Created: April 2025
'
' This module contains the main procedures for the manufacturing workflow scheduler,
' including data import, workflow processing, and schedule generation.
Option Explicit
' Global constants
Public Const MODULE_NAME As String = "MainModule"
Public Const VERSION As String = "1.0"
' ===== Main Functions =====
' Show the main user form
Public Sub ShowMainForm()
frmMain.Show
End Sub
' Import data from CSV file
Public Sub ImportCSVData(Optional filePath As String = "")
Dim ws As Worksheet
Dim importFilePath As String
Dim fileNum As Integer
Dim dataLine As String
Dim dataValues() As String
Dim row As Long
Dim col As Long
On Error GoTo ErrorHandler
' Set reference to the data sheet
Set ws = ThisWorkbook.Sheets("ImportedData")
' Clear existing data
ws.UsedRange.ClearContents
' Get file path if not provided
If filePath = "" Then
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select CSV file to import"
.Filters.Add "CSV Files", "*.csv"
.AllowMultiSelect = False
If .Show = -1 Then
importFilePath = .SelectedItems(1)
Else
MsgBox "Import cancelled.", vbInformation
Exit Sub
End If
End With
Else
importFilePath = filePath
End If
' Write headers
ws.Range("A1").Value = "WorkOrder"
ws.Range("B1").Value = "Product"
ws.Range("C1").Value = "Quantity"
ws.Range("D1").Value = "StartDate"
ws.Range("E1").Value = "DueDate"
ws.Range("F1").Value = "Priority"
' Import the data
fileNum = FreeFile
Open importFilePath For Input As #fileNum
' Skip header row
Line Input #fileNum, dataLine
' Read data rows
row = 2
Do Until EOF(fileNum)
Line Input #fileNum, dataLine
dataValues = Split(dataLine, ",")
For col = 0 To UBound(dataValues)
ws.Cells(row, col + 1).Value = dataValues(col)
Next col
row = row + 1
Loop
Close #fileNum
' Format the data
FormatImportedData ws
' Success message
MsgBox "Data imported successfully.", vbInformation
Exit Sub
ErrorHandler:
If fileNum > 0 Then Close #fileNum
MsgBox "Error importing data: " & Err.Description, vbExclamation
End Sub
' Format the imported data
Private Sub FormatImportedData(ws As Worksheet)
' Format headers
With ws.Range("A1:F1")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
End With
' Format date columns
With ws.Range("D:E")
.NumberFormat = "yyyy-mm-dd"
End With
' AutoFit columns
ws.UsedRange.Columns.AutoFit
' Create a table for easier filtering
If Not ws.ListObjects.Count > 0 Then
ws.ListObjects.Add(xlSrcRange, ws.UsedRange, , xlYes).Name = "ImportedDataTable"
Else
ws.ListObjects(1).Resize ws.UsedRange
End If
End Sub
' Import workflow definition from CSV
Public Sub ImportWorkflowFromCSV(Optional filePath As String = "")
Dim ws As Worksheet
Dim importFilePath As String
Dim fileNum As Integer
Dim dataLine As String
Dim dataValues() As String
Dim row As Long
Dim col As Long
On Error GoTo ErrorHandler
' Set reference to the workflow sheet
Set ws = ThisWorkbook.Sheets("WorkflowSetup")
' Clear existing data
ws.UsedRange.ClearContents
' Get file path if not provided
If filePath = "" Then
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select Workflow CSV file to import"
.Filters.Add "CSV Files", "*.csv"
.AllowMultiSelect = False
If .Show = -1 Then
importFilePath = .SelectedItems(1)
Else
MsgBox "Import cancelled.", vbInformation
Exit Sub
End If
End With
Else
importFilePath = filePath
End If
' Write headers
ws.Range("A1").Value = "StepID"
ws.Range("B1").Value = "StepName"
ws.Range("C1").Value = "Department"
ws.Range("D1").Value = "Duration"
ws.Range("E1").Value = "Prerequisites"
ws.Range("F1").Value = "ResourceRequired"
ws.Range("G1").Value = "SetupTime"
' Import the data
fileNum = FreeFile
Open importFilePath For Input As #fileNum
' Skip header row
Line Input #fileNum, dataLine
' Read data rows
row = 2
Do Until EOF(fileNum)
Line Input #fileNum, dataLine
dataValues = Split(dataLine, ",")
For col = 0 To UBound(dataValues)
ws.Cells(row, col + 1).Value = dataValues(col)
Next col
row = row + 1
Loop
Close #fileNum
' Format the workflow data
FormatWorkflowData ws
' Success message
MsgBox "Workflow definition imported successfully.", vbInformation
Exit Sub
ErrorHandler:
If fileNum > 0 Then Close #fileNum
MsgBox "Error importing workflow: " & Err.Description, vbExclamation
End Sub
' Format the workflow data
Private Sub FormatWorkflowData(ws As Worksheet)
' Format headers
With ws.Range("A1:G1")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
End With
' Format duration and setup time as numbers
ws.Range("D:D,G:G").NumberFormat = "0.00"
' AutoFit columns
ws.UsedRange.Columns.AutoFit
' Create a table for easier filtering
If Not ws.ListObjects.Count > 0 Then
ws.ListObjects.Add(xlSrcRange, ws.UsedRange, , xlYes).Name = "WorkflowTable"
Else
ws.ListObjects(1).Resize ws.UsedRange
End If
End Sub
' Generate the schedule based on workflow and imported data
Public Sub GenerateSchedule()
Dim wsImport As Worksheet
Dim wsWorkflow As Worksheet
Dim wsSchedule As Worksheet
Dim wsParams As Worksheet
Dim lastRow As Long
Dim i As Long, j As Long
Dim workOrders As Collection
Dim workflowSteps As Collection
Dim startDate As Date
Dim workingHoursPerDay As Double
Dim useCalendarDays As Boolean
On Error GoTo ErrorHandler
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Set references to sheets
Set wsImport = ThisWorkbook.Sheets("ImportedData")
Set wsWorkflow = ThisWorkbook.Sheets("WorkflowSetup")
Set wsSchedule = ThisWorkbook.Sheets("Schedule")
Set wsParams = ThisWorkbook.Sheets("Parameters")
' Get scheduling parameters
startDate = IIf(IsDate(wsParams.Range("B2").Value), wsParams.Range("B2").Value, Date)
workingHoursPerDay = IIf(IsNumeric(wsParams.Range("B3").Value), wsParams.Range("B3").Value, 8)
useCalendarDays = (wsParams.Range("B4").Value = "Yes")
' Clear existing schedule
wsSchedule.UsedRange.ClearContents
' Set up schedule headers
wsSchedule.Range("A1").Value = "WorkOrder"
wsSchedule.Range("B1").Value = "Product"
wsSchedule.Range("C1").Value = "StepID"
wsSchedule.Range("D1").Value = "StepName"
wsSchedule.Range("E1").Value = "Department"
wsSchedule.Range("F1").Value = "ResourceRequired"
wsSchedule.Range("G1").Value = "StartDate"
wsSchedule.Range("H1").Value = "EndDate"
wsSchedule.Range("I1").Value = "Duration"
wsSchedule.Range("J1").Value = "Status"
' Format headers
With wsSchedule.Range("A1:J1")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
End With
' Get work orders
Set workOrders = GetWorkOrders(wsImport)
' Get workflow steps
Set workflowSteps = GetWorkflowSteps(wsWorkflow)
' Generate schedule for each work order
Dim rowCounter As Long
rowCounter = 2
For Each wo In workOrders
Dim woSteps As Collection
Set woSteps = BuildWorkOrderSchedule(wo, workflowSteps, startDate, workingHoursPerDay, useCalendarDays)
' Write work order schedule to the sheet
For Each step In woSteps
wsSchedule.Cells(rowCounter, 1).Value = wo("WorkOrder")
wsSchedule.Cells(rowCounter, 2).Value = wo("Product")
wsSchedule.Cells(rowCounter, 3).Value = step("StepID")
wsSchedule.Cells(rowCounter, 4).Value = step("StepName")
wsSchedule.Cells(rowCounter, 5).Value = step("Department")
wsSchedule.Cells(rowCounter, 6).Value = step("ResourceRequired")
wsSchedule.Cells(rowCounter, 7).Value = step("StartDate")
wsSchedule.Cells(rowCounter, 8).Value = step("EndDate")
wsSchedule.Cells(rowCounter, 9).Value = step("Duration")
wsSchedule.Cells(rowCounter, 10).Value = "Scheduled"
rowCounter = rowCounter + 1
Next step
Next wo
' Format schedule
With wsSchedule.Range("G:H")
.NumberFormat = "yyyy-mm-dd"
End With
wsSchedule.UsedRange.Columns.AutoFit
' Create table
If wsSchedule.ListObjects.Count > 0 Then
wsSchedule.ListObjects(1).Resize wsSchedule.UsedRange
Else
wsSchedule.ListObjects.Add(xlSrcRange, wsSchedule.UsedRange, , xlYes).Name = "ScheduleTable"
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Schedule generated successfully.", vbInformation
Exit Sub
ErrorHandler:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Error generating schedule: " & Err.Description, vbExclamation
End Sub
' Get work orders from imported data
Private Function GetWorkOrders(ws As Worksheet) As Collection
Dim workOrders As New Collection
Dim lastRow As Long
Dim i As Long
Dim wo As Object
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
Set wo = CreateObject("Scripting.Dictionary")
wo.Add "WorkOrder", ws.Cells(i, 1).Value
wo.Add "Product", ws.Cells(i, 2).Value
wo.Add "Quantity", ws.Cells(i, 3).Value
wo.Add "StartDate", ws.Cells(i, 4).Value
wo.Add "DueDate", ws.Cells(i, 5).Value
wo.Add "Priority", ws.Cells(i, 6).Value
workOrders.Add wo
Next i
Set GetWorkOrders = workOrders
End Function
' Get workflow steps from workflow setup
Private Function GetWorkflowSteps(ws As Worksheet) As Collection
Dim workflowSteps As New Collection
Dim lastRow As Long
Dim i As Long
Dim step As Object
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
Set step = CreateObject("Scripting.Dictionary")
step.Add "StepID", ws.Cells(i, 1).Value
step.Add "StepName", ws.Cells(i, 2).Value
step.Add "Department", ws.Cells(i, 3).Value
step.Add "Duration", ws.Cells(i, 4).Value
step.Add "Prerequisites", ws.Cells(i, 5).Value
step.Add "ResourceRequired", ws.Cells(i, 6).Value
step.Add "SetupTime", ws.Cells(i, 7).Value
workflowSteps.Add step, step("StepID")
Next i
Set GetWorkflowSteps = workflowSteps
End Function
' Build schedule for a work order
Private Function BuildWorkOrderSchedule(wo As Object, workflowSteps As Collection, startDate As Date, workingHoursPerDay As Double, useCalendarDays As Boolean) As Collection
Dim woSchedule As New Collection
Dim scheduledSteps As New Collection
Dim allPrerequisites As New Collection
Dim step As Object
Dim i As Long
Dim currentStartDate As Date
currentStartDate = IIf(IsDate(wo("StartDate")), wo("StartDate"), startDate)
' Get all steps that don't have prerequisites
For Each step In workflowSteps
If Trim(step("Prerequisites")) = "" Then
Dim newStep As Object
Set newStep = CloneStep(step)
' Calculate start and end dates
newStep("StartDate") = currentStartDate
newStep("EndDate") = CalculateEndDate(currentStartDate, CDbl(newStep("Duration")), CDbl(newStep("SetupTime")), workingHoursPerDay, useCalendarDays)
woSchedule.Add newStep
scheduledSteps.Add newStep("StepID")
Else
' Collect all unique prerequisites
Dim prereqs As Variant
prereqs = Split(step("Prerequisites"), ",")
For i = 0 To UBound(prereqs)
Dim prereq As String
prereq = Trim(prereqs(i))
On Error Resume Next
allPrerequisites.Add prereq, prereq
On Error GoTo 0
Next i
End If
Next step
' Process remaining steps until all are scheduled
Do While scheduledSteps.Count < workflowSteps.Count
Dim progress As Boolean
progress = False
For Each step In workflowSteps
' Skip already scheduled steps
On Error Resume Next
Dim testStep As String
testStep = scheduledSteps(step("StepID"))
If Err.Number = 0 Then
On Error GoTo 0
GoTo NextStep
End If
On Error GoTo 0
' Check if all prerequisites are scheduled
Dim canSchedule As Boolean
canSchedule = True
If Trim(step("Prerequisites")) <> "" Then
prereqs = Split(step("Prerequisites"), ",")
For i = 0 To UBound(prereqs)
prereq = Trim(prereqs(i))
On Error Resume Next
testStep = scheduledSteps(prereq)
If Err.Number <> 0 Then
canSchedule = False
Exit For
End If
On Error GoTo 0
Next i
End If
If canSchedule Then
Set newStep = CloneStep(step)
' Find latest end date of prerequisites
Dim latestDate As Date
latestDate = currentStartDate
If Trim(step("Prerequisites")) <> "" Then
prereqs = Split(step("Prerequisites"), ",")
For i = 0 To UBound(prereqs)
prereq = Trim(prereqs(i))
For Each scheduledStep In woSchedule
If scheduledStep("StepID") = prereq Then
If scheduledStep("EndDate") > latestDate Then
latestDate = scheduledStep("EndDate")
End If
Exit For
End If
Next scheduledStep
Next i
End If
' Calculate start and end dates
newStep("StartDate") = latestDate
newStep("EndDate") = CalculateEndDate(latestDate, CDbl(newStep("Duration")), CDbl(newStep("SetupTime")), workingHoursPerDay, useCalendarDays)
woSchedule.Add newStep
scheduledSteps.Add newStep("StepID"), newStep("StepID")
progress = True
End If
NextStep:
Next step
' If no progress was made, we might have a circular dependency
If Not progress Then
MsgBox "Warning: Possible circular dependency in workflow. Not all steps could be scheduled.", vbExclamation
Exit Do
End If
Loop
Set BuildWorkOrderSchedule = woSchedule
End Function
' Clone a step dictionary
Private Function CloneStep(step As Object) As Object
Dim newStep As Object
Set newStep = CreateObject("Scripting.Dictionary")
Dim key As Variant
For Each key In step.Keys
newStep.Add key, step(key)
Next key
Set CloneStep = newStep
End Function
' Calculate end date based on start date and duration
Private Function CalculateEndDate(startDate As Date, duration As Double, setupTime As Double, workingHoursPerDay As Double, useCalendarDays As Boolean) As Date
Dim totalDays As Double
Dim endDate As Date
' Total hours needed (duration + setup)
Dim totalHours As Double
totalHours = duration + setupTime
' Calculate days needed
totalDays = totalHours / workingHoursPerDay
If useCalendarDays Then
' Simple calendar days calculation
endDate = startDate + Int(totalDays)
' Add partial day if needed
If totalDays - Int(totalDays) > 0 Then
endDate = endDate + 1
End If
Else
' Working days calculation (excluding weekends)
Dim daysToAdd As Integer
daysToAdd = Int(totalDays)
endDate = startDate
' Add full days
For i = 1 To daysToAdd
endDate = endDate + 1
' Skip weekends
Do While Weekday(endDate) = vbSaturday Or Weekday(endDate) = vbSunday
endDate = endDate + 1
Loop
Next i
' Add partial day if needed
If totalDays - Int(totalDays) > 0 Then
endDate = endDate + 1
' Skip weekends
Do While Weekday(endDate) = vbSaturday Or Weekday(endDate) = vbSunday
endDate = endDate + 1
Loop
End If
End If
CalculateEndDate = endDate
End Function
' Export generated schedule to CSV
Public Sub ExportScheduleToCSV()
Dim ws As Worksheet
Dim exportFilePath As String
Dim fileNum As Integer
Dim i As Long, j As Long
Dim lastRow As Long, lastCol As Long
Dim line As String
On Error GoTo ErrorHandler
' Set reference to the schedule sheet
Set ws = ThisWorkbook.Sheets("Schedule")
' Get file path for export
With Application.FileDialog(msoFileDialogSaveAs)
.Title = "Save Schedule As CSV"
.InitialFileName = "Manufacturing_Schedule.csv"
.Filters.Add "CSV Files", "*.csv"
If .Show = -1 Then
exportFilePath = .SelectedItems(1)
Else
MsgBox "Export cancelled.", vbInformation
Exit Sub
End If
End With
' Get data range
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
' Open file for writing
fileNum = FreeFile
Open exportFilePath For Output As #fileNum
' Write data
For i = 1 To lastRow
line = ""
For j = 1 To lastCol
If j > 1 Then line = line & ","
' Format dates as ISO format
If j = 7 Or j = 8 Then ' StartDate and EndDate columns
If IsDate(ws.Cells(i, j).Value) Then
line = line & Format(ws.Cells(i, j).Value, "yyyy-mm-dd")
Else
line = line & ws.Cells(i, j).Value
End If
Else
' Handle text with commas by adding quotes
If InStr(ws.Cells(i, j).Value, ",") > 0 Then
line = line & """" & ws.Cells(i, j).Value & """"
Else
line = line & ws.Cells(i, j).Value
End If
End If
Next j
Print #fileNum, line
Next i
Close #fileNum
MsgBox "Schedule exported successfully to " & exportFilePath, vbInformation
Exit Sub
ErrorHandler:
If fileNum > 0 Then Close #fileNum
MsgBox "Error exporting schedule: " & Err.Description, vbExclamation
End Sub
' Auto-run when workbook opens
Public Sub Workbook_Open()
' Check if sheets exist, create if needed
CreateRequiredSheets
' Apply default settings
ApplyDefaultSettings
End Sub
' Create required sheets if they don't exist
Private Sub CreateRequiredSheets()
Dim sheetNames As Variant
Dim i As Long
sheetNames = Array("Dashboard", "ImportedData", "WorkflowSetup", "Parameters", "Schedule", "Reports")
For i = LBound(sheetNames) To UBound(sheetNames)
If Not SheetExists(sheetNames(i)) Then
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = sheetNames(i)
End If
Next i
End Sub
' Check if a sheet exists
Private Function SheetExists(sheetName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0
SheetExists = Not ws Is Nothing
End Function
' Apply default settings to sheets
Private Sub ApplyDefaultSettings()
' Parameters sheet
With ThisWorkbook.Sheets("Parameters")
If .Range("A1").Value = "" Then
.Range("A1").Value = "Parameter"
.Range("B1").Value = "Value"
.Range("A2").Value = "Start Date"
.Range("B2").Value = Date
.Range("A3").Value = "Working Hours Per Day"
.Range("B3").Value = 8
.Range("A4").Value = "Use Calendar Days"
.Range("B4").Value = "No"
.Range("A5").Value = "Max Resource Utilization %"
.Range("B5").Value = 90
' Format
.Range("A1:B1").Font.Bold = True
.Range("A:B").Columns.AutoFit
End If
End With
' Dashboard sheet setup
SetupDashboard
End Sub
' Set up the dashboard
Private Sub SetupDashboard()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Dashboard")
' Clear sheet
ws.UsedRange.Clear
' Title
ws.Range("B2").Value = "Manufacturing Workflow Scheduler"
ws.Range("B2").Font.Size = 16
ws.Range("B2").Font.Bold = True
' Import section
ws.Range("B4").Value = "Data Import"
ws.Range("B4").Font.Bold = True
' Add buttons
If Not ButtonExists(ws, "ImportDataBtn") Then
ws.Buttons.Add(100, 100, 150, 30).Select
With Selection
.Name = "ImportDataBtn"
.Caption = "Import Data from CSV"
.OnAction = "ImportCSVData"
.Left = ws.Range("B5").Left
.Top = ws.Range("B5").Top
.Width = 150
.Height = 25
End With
End If
If Not ButtonExists(ws, "ImportWorkflowBtn") Then
ws.Buttons.Add(100, 140, 150, 30).Select
With Selection
.Name = "ImportWorkflowBtn"
.Caption = "Import Workflow from CSV"
.OnAction = "ImportWorkflowFromCSV"
.Left = ws.Range("B7").Left
.Top = ws.Range("B7").Top
.Width = 150
.Height = 25
End With
End If
' Schedule section
ws.Range("B9").Value = "Schedule Generation"
ws.Range("B9").Font.Bold = True
If Not ButtonExists(ws, "GenerateScheduleBtn") Then
ws.Buttons.Add(100, 180, 150, 30).Select
With Selection
.Name = "GenerateScheduleBtn"
.Caption = "Generate Schedule"
.OnAction = "GenerateSchedule"
.Left = ws.Range("B10").Left
.Top = ws.Range("B10").Top
.Width = 150
.Height = 25
End With
End If
' Export section
ws.Range("B12").Value = "Export"
ws.Range("B12").Font.Bold = True
If Not ButtonExists(ws, "ExportScheduleBtn") Then
ws.Buttons.Add(100, 220, 150, 30).Select
With Selection
.Name = "ExportScheduleBtn"
.Caption = "Export Schedule to CSV"
.OnAction = "ExportScheduleToCSV"
.Left = ws.Range("B13").Left
.Top = ws.Range("B13").Top
.Width = 150
.Height = 25
End With
End If
' Instructions
ws.Range("E4").Value = "Instructions:"
ws.Range("E4").Font.Bold = True
ws.Range("E5").Value = "1. Import your ERP data using the 'Import Data' button"
ws.Range("E6").Value = "2. Import or set up workflow in the 'WorkflowSetup' sheet"
ws.Range("E7").Value = "3. Configure parameters in the 'Parameters' sheet"
ws.Range("E8").Value = "4. Generate the schedule"
ws.Range("E9").Value = "5. Review the results in the 'Schedule' sheet"
ws.Range("E10").Value = "6. Export to CSV if needed"
' Version
ws.Range("B15").Value = "Version: " & VERSION
' Deselect
ws.Range("A1").Select
End Sub
' Check if button exists
Private Function ButtonExists(ws As Worksheet, buttonName As String) As Boolean
Dim btn As Button
On Error Resume Next
Set btn = ws.Buttons(buttonName)
On Error GoTo 0
ButtonExists = Not btn Is Nothing
End Function