VBA Code for Audit Schedule Booking Tool
VBA Code for Audit Schedule Booking Tool
' --- Initialize dictionaries to track audit dates and last session row per
audit day ---
Set lastSessionRow = CreateObject("Scripting.Dictionary")
Set auditDates = CreateObject("Scripting.Dictionary")
' --- Allow the user to browse for the workbook ---
filePath = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", ,
"Select the schedule workbook")
If filePath = "False" Then Exit Sub ' User canceled
Set wb = Workbooks.Open(filePath)
If wsIndex = 0 Then
MsgBox "No worksheet selected. Operation canceled.", vbExclamation
wb.Close False
Exit Sub
End If
If ws Is Nothing Then
MsgBox "The selected worksheet does not exist in the workbook. Please try
again.", vbExclamation
wb.Close False
Exit Sub
End If
' --- Determine last row in the worksheet (using session number column) ---
lastRow = ws.Cells(ws.Rows.Count, COL_SESSION_NUMBER).End(xlUp).Row
If lastRow < START_DATA_ROW Then
MsgBox "The worksheet does not contain sufficient data.", vbExclamation
wb.Close False
Exit Sub
End If
' --- First pass: identify audit dates and last session row for each day ---
For i = START_DATA_ROW To lastRow
If IsEmpty(ws.Cells(i, COL_SESSION_NUMBER).Value) Or IsEmpty(ws.Cells(i,
COL_SESSION_TIME).Value) Then Exit For
' --- Main loop: Create session meetings and schedule wrap-up meetings ---
meetingScheduled = False
For i = START_DATA_ROW To lastRow
If IsEmpty(ws.Cells(i, COL_SESSION_NUMBER).Value) Or IsEmpty(ws.Cells(i,
COL_SESSION_TIME).Value) Then Exit For
' Assumes wrap-up time is in the row immediately after the last session.
timeRange = Trim(ws.Cells(i + 1, COL_SESSION_TIME).Value)
If InStr(timeRange, "-") > 0 Then
timeParts = Split(timeRange, "-")
On Error Resume Next
wrapUpStartTime = CDate(auditDate & " " & Trim(timeParts(0)))
wrapUpEndTime = CDate(auditDate & " " & Trim(timeParts(1)))
On Error GoTo 0
Dim i As Integer
For i = 1 To Len(separators)
temp = Mid(separators, i, 1)
cleanedString = Replace(cleanedString, temp, ";")
Next i
' === HELPER FUNCTION: Get Recipients for a Given Audit Day ===
Function GetRecipientsForDay(ws As Worksheet, auditDate As Date, lastRow As Long)
As Collection
Dim recipientList As New Collection
Dim i As Long, timeRange As String
Dim currentAuditDate As Date
Dim timeParts() As String
' Now, if the currentAuditDate matches the auditDate we're looking for,
' and the row is a session row (indicated by a session number in the
session number column),
' then we process it.
If currentAuditDate = auditDate And ws.Cells(i, COL_SESSION_NUMBER).Value >
0 Then
timeRange = Trim(ws.Cells(i, COL_SESSION_TIME).Value)
If InStr(timeRange, "-") > 0 Then
timeParts = Split(timeRange, "-")
' We can skip validating times here if it's not needed for
collecting recipients
Dim recipients As String
recipients = Trim(ws.Cells(i, COL_AUDITOR).Value) & ";" &
Trim(ws.Cells(i, COL_AUDITEE).Value)
Dim recipientArray() As String, recipient As Variant
recipientArray = ParseRecipients(recipients)
For Each recipient In recipientArray
If Trim(recipient) <> "" Then
On Error Resume Next
recipientList.Add CleanRecipient(Trim(recipient)),
CleanRecipient(Trim(recipient))
On Error GoTo 0
End If
Next recipient
End If
End If
Next i
Set GetRecipientsForDay = recipientList
End Function