-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserFormModule.bas
More file actions
306 lines (249 loc) · 9.13 KB
/
Copy pathUserFormModule.bas
File metadata and controls
306 lines (249 loc) · 9.13 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
Attribute VB_Name = "UserFormModule"
' Manufacturing Workflow Scheduler - User Form Module
' Created: April 2025
'
' This module contains code for handling user form interactions
Option Explicit
' Show the workflow visualization form
Public Sub ShowWorkflowVisualization()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
' Reference to workflow sheet
Set ws = ThisWorkbook.Sheets("WorkflowSetup")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Create form
frmWorkflowViz.Show
End Sub
' Setup the workflow visualization form
Public Sub SetupWorkflowVisualizationForm(frm As Object)
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim steps As Collection
Dim connections As Collection
' Reference to workflow sheet
Set ws = ThisWorkbook.Sheets("WorkflowSetup")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Set steps = New Collection
Set connections = New Collection
' Get steps and connections
For i = 2 To lastRow
Dim stepID As String
Dim stepName As String
Dim prerequisites As String
stepID = ws.Cells(i, 1).Value
stepName = ws.Cells(i, 2).Value
prerequisites = ws.Cells(i, 5).Value
' Add step
Dim step As Object
Set step = CreateObject("Scripting.Dictionary")
step.Add "ID", stepID
step.Add "Name", stepName
steps.Add step
' Add connections
If Trim(prerequisites) <> "" Then
Dim prereqs As Variant
prereqs = Split(prerequisites, ",")
Dim j As Long
For j = 0 To UBound(prereqs)
Dim conn As Object
Set conn = CreateObject("Scripting.Dictionary")
conn.Add "From", Trim(prereqs(j))
conn.Add "To", stepID
connections.Add conn
Next j
End If
Next i
' Draw workflow on form
DrawWorkflow frm, steps, connections
End Sub
' Draw workflow on form
Private Sub DrawWorkflow(frm As Object, steps As Collection, connections As Collection)
' This would be the implementation for drawing the workflow diagram
' For now, we'll leave this as a placeholder
' In a real implementation, this would use VBA graphics objects to create
' a visual representation of the workflow with boxes and arrows
' Draw steps
Dim i As Long
Dim xPos As Long, yPos As Long
Dim boxWidth As Long, boxHeight As Long
boxWidth = 100
boxHeight = 50
xPos = 50
yPos = 50
For i = 1 To steps.Count
' Draw box
frm.Controls.Add "Forms.Label.1", "Step" & i, True
With frm.Controls("Step" & i)
.Left = xPos
.Top = yPos
.Width = boxWidth
.Height = boxHeight
.Caption = steps(i)("ID") & ": " & steps(i)("Name")
.BorderStyle = 1 ' fmBorderStyleSingle
End With
' Update position for next box
yPos = yPos + boxHeight + 30
' Reset y position and move x position if we're at the bottom of the form
If yPos > frm.InsideHeight - boxHeight Then
yPos = 50
xPos = xPos + boxWidth + 50
End If
Next i
' In a real implementation, we would also draw lines for connections
End Sub
' Update resource utilization chart
Public Sub UpdateResourceChart()
Dim wsSchedule As Worksheet
Dim wsChart As Worksheet
Dim lastRow As Long
Dim resources As Collection
Dim i As Long
' Get references to sheets
Set wsSchedule = ThisWorkbook.Sheets("Schedule")
' Create chart sheet if it doesn't exist
If Not SheetExists("ResourceChart") Then
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = "ResourceChart"
End If
Set wsChart = ThisWorkbook.Sheets("ResourceChart")
' Clear chart sheet
wsChart.UsedRange.Clear
' Get data from schedule
lastRow = wsSchedule.Cells(wsSchedule.Rows.Count, 1).End(xlUp).Row
' Get unique resources
Set resources = New Collection
On Error Resume Next
For i = 2 To lastRow
resources.Add wsSchedule.Cells(i, 6).Value, CStr(wsSchedule.Cells(i, 6).Value)
Next i
On Error GoTo 0
' Create resource utilization data
wsChart.Range("A1").Value = "Resource"
wsChart.Range("B1").Value = "Total Hours"
Dim row As Long
row = 2
Dim resource As Variant
For Each resource In resources
wsChart.Cells(row, 1).Value = resource
' Calculate total hours for resource
Dim totalHours As Double
totalHours = 0
For i = 2 To lastRow
If wsSchedule.Cells(i, 6).Value = resource Then
totalHours = totalHours + wsSchedule.Cells(i, 9).Value
End If
Next i
wsChart.Cells(row, 2).Value = totalHours
row = row + 1
Next resource
' Format
wsChart.Range("A1:B1").Font.Bold = True
wsChart.UsedRange.Columns.AutoFit
' Create chart
If wsChart.ChartObjects.Count > 0 Then
wsChart.ChartObjects(1).Delete
End If
Dim chartObj As ChartObject
Set chartObj = wsChart.ChartObjects.Add(100, 100, 400, 300)
With chartObj.Chart
.SetSourceData Source:=wsChart.Range("A1:B" & row - 1)
.ChartType = xlColumnClustered
.HasTitle = True
.ChartTitle.Text = "Resource Utilization (Hours)"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "Hours"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Resource"
End With
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
' Generate a schedule summary report
Public Sub GenerateScheduleSummary()
Dim wsSchedule As Worksheet
Dim wsReport As Worksheet
Dim lastRow As Long
Dim workOrders As Collection
Dim i As Long
' Get references to sheets
Set wsSchedule = ThisWorkbook.Sheets("Schedule")
Set wsReport = ThisWorkbook.Sheets("Reports")
' Clear report sheet
wsReport.UsedRange.Clear
' Get data from schedule
lastRow = wsSchedule.Cells(wsSchedule.Rows.Count, 1).End(xlUp).Row
' Get unique work orders
Set workOrders = New Collection
On Error Resume Next
For i = 2 To lastRow
workOrders.Add wsSchedule.Cells(i, 1).Value, CStr(wsSchedule.Cells(i, 1).Value)
Next i
On Error GoTo 0
' Create summary headers
wsReport.Range("A1").Value = "Work Order"
wsReport.Range("B1").Value = "Product"
wsReport.Range("C1").Value = "Earliest Start"
wsReport.Range("D1").Value = "Latest Finish"
wsReport.Range("E1").Value = "Total Duration"
wsReport.Range("F1").Value = "Step Count"
Dim row As Long
row = 2
Dim workOrder As Variant
For Each workOrder In workOrders
Dim woProduct As String
Dim earliestStart As Date
Dim latestFinish As Date
Dim totalDuration As Double
Dim stepCount As Long
' Initialize values
earliestStart = DateValue("2099-12-31")
latestFinish = DateValue("1900-01-01")
totalDuration = 0
stepCount = 0
' Find data for this work order
For i = 2 To lastRow
If wsSchedule.Cells(i, 1).Value = workOrder Then
' Get product
woProduct = wsSchedule.Cells(i, 2).Value
' Check earliest start
If wsSchedule.Cells(i, 7).Value < earliestStart Then
earliestStart = wsSchedule.Cells(i, 7).Value
End If
' Check latest finish
If wsSchedule.Cells(i, 8).Value > latestFinish Then
latestFinish = wsSchedule.Cells(i, 8).Value
End If
' Add duration
totalDuration = totalDuration + wsSchedule.Cells(i, 9).Value
' Count step
stepCount = stepCount + 1
End If
Next i
' Write summary row
wsReport.Cells(row, 1).Value = workOrder
wsReport.Cells(row, 2).Value = woProduct
wsReport.Cells(row, 3).Value = earliestStart
wsReport.Cells(row, 4).Value = latestFinish
wsReport.Cells(row, 5).Value = totalDuration
wsReport.Cells(row, 6).Value = stepCount
row = row + 1
Next workOrder
' Format
wsReport.Range("A1:F1").Font.Bold = True
wsReport.Range("C:D").NumberFormat = "yyyy-mm-dd"
wsReport.Range("E:E").NumberFormat = "0.00"
wsReport.UsedRange.Columns.AutoFit
' Create table
If Not wsReport.ListObjects.Count > 0 Then
wsReport.ListObjects.Add(xlSrcRange, wsReport.UsedRange, , xlYes).Name = "SummaryTable"
Else
wsReport.ListObjects(1).Resize wsReport.UsedRange
End If
End Sub