Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. Public Function IsInArray(intToBeFound As Integer, arr As Variant) As Boolean
  2. Dim i
  3. For i = LBound(arr) To UBound(arr)
  4. If arr(i) = intToBeFound Then
  5. IsInArray = True
  6. Exit Function
  7. End If
  8. Next i
  9. IsInArray = False
  10.  
  11. End Function
  12.  
  13. Function IsInteger(ByVal Value As Variant) As Boolean
  14. IsInteger = IIf(Int(Value) = Value, CInt(Value), False)
  15. End Function
  16.  
  17. Sub DataColorIntervals()
  18.  
  19. ' Range Variables
  20. Dim time As String
  21. time = "00"
  22. Dim timeLast As String
  23. timeLast = "00"
  24. Dim timeRange As Integer
  25. timeRange = 0
  26. Dim timeFirst As String
  27. timeFirst = "00"
  28.  
  29. Dim Count As Integer
  30. Dim flipCount As Integer
  31. flipCount = 0
  32. Dim colorFlip As Boolean
  33. colorFlip = False
  34.  
  35. ' Table format rows
  36. Dim startRow As Integer
  37. startRow = 1
  38.  
  39. Dim maxCalls As Integer
  40. Dim maxCallsRow As Integer
  41. Dim maxRange
  42.  
  43. ' Time intervals
  44. Dim flipArr(11) As Integer ' 5 Minutes
  45. For Z = 1 To UBound(flipArr)
  46. flipArr(Z) = Z * 5
  47. Next Z
  48.  
  49. 'Dim flipArr(4) As Integer ' 15 Minutes
  50. 'For Z = 0 To UBound(flipArr)
  51. ' flipArr(Z) = Z + 15
  52. 'Next Z
  53.  
  54. Dim flipArr15(3) As Integer
  55. For Z = 1 To UBound(flipArr15)
  56. flipArr15(Z) = Z * 5
  57. Next Z
  58.  
  59. Dim lastRow As Integer
  60.  
  61. Dim wsNum As Integer
  62. wsNum = ThisWorkbook.Worksheets.Count
  63.  
  64. Dim starting_ws As Worksheet
  65. Set starting_ws = ActiveSheet 'remember which worksheet is active in the beginning
  66. ThisWorkbook.Worksheets(3).Columns("H:N").Clear
  67.  
  68.  
  69. ' --------------------------------------------------------------------
  70. ' Workbook loop
  71. For x = 4 To wsNum
  72. ' ThisWorkbook.Worksheets(x).Activate
  73. ' MsgBox ("Hi")
  74. lastRow = ThisWorkbook.Worksheets(x).Cells(Rows.Count, "A").End(xlUp).Row
  75. ThisWorkbook.Worksheets(x).Cells.ClearFormats
  76. ThisWorkbook.Worksheets(x).Columns(11).Clear
  77. timeFirst = "00"
  78. maxCalls = 0
  79. maxCallsRow = 1
  80.  
  81. Count = 2
  82. flipCount = 0
  83.  
  84. ' --------------------------------------------------------------------
  85. ' Table Setup
  86. ThisWorkbook.Worksheets(3).Cells(startRow, 9).Interior.ColorIndex = 17
  87. ThisWorkbook.Worksheets(3).Cells(startRow, 10) = "Average Wait" + vbNewLine + "in Interval"
  88. ThisWorkbook.Worksheets(3).Cells(startRow, 10).Interior.ColorIndex = 17
  89. ThisWorkbook.Worksheets(3).Cells(startRow, 11) = "Maximum Wait" + vbNewLine + "in Interval"
  90. ThisWorkbook.Worksheets(3).Cells(startRow, 11).Interior.ColorIndex = 17
  91. ThisWorkbook.Worksheets(3).Cells(startRow, 12) = "Time to Destination" + vbNewLine + "in Interval"
  92. ThisWorkbook.Worksheets(3).Cells(startRow, 12).Interior.ColorIndex = 17
  93. ThisWorkbook.Worksheets(3).Cells(startRow, 13) = "Number of Calls" + vbNewLine + "in Interval"
  94. ThisWorkbook.Worksheets(3).Cells(startRow, 13).Interior.ColorIndex = 17
  95.  
  96. ThisWorkbook.Worksheets(3).Cells(startRow, 8) = Mid(ThisWorkbook.Worksheets(x).Cells(startRow + 1, 1), 1, 10) & " " & Mid(ThisWorkbook.Worksheets(x).Cells(startRow + 1, 1), 21, 4)
  97. ThisWorkbook.Worksheets(3).Cells(startRow, 8).Interior.ColorIndex = 17
  98. ThisWorkbook.Worksheets(3).Range("H" & CStr(startRow + 1) & ":" & "M" & CStr(startRow + 4)).Interior.ColorIndex = 37
  99. ' --------------------------------------------------------------------
  100. ' Interval highlights
  101. While Count < lastRow + 1
  102.  
  103. timeLast = time
  104. time = Mid(ThisWorkbook.Worksheets(x).Cells(Count, 1), 15, 2)
  105.  
  106. ' MsgBox (time)
  107. '' Check for 15 minute intervals
  108. 'If Mid(Time, 15, 2) = "00" And Mid(Time, 13, 4) <> Mid(TimeLast, 13, 4) Then
  109. ' colorFlip = Not colorFlip
  110. 'ElseIf Mid(Time, 15, 2) = "15" And Mid(Time, 13, 4) <> Mid(TimeLast, 13, 4) Then
  111. ' colorFlip = Not colorFlip
  112. 'ElseIf Mid(Time, 15, 2) = "30" And Mid(Time, 13, 4) <> Mid(TimeLast, 13, 4) Then
  113. ' colorFlip = Not colorFlip
  114. 'ElseIf Mid(Time, 15, 2) = "45" And Mid(Time, 13, 4) <> Mid(TimeLast, 13, 4) Then
  115. ' colorFlip = Not colorFlip
  116. 'Else
  117. ' End If
  118.  
  119. '' Check for 5 minute intervals
  120. timeRange = CInt(time) - CInt(timeFirst)
  121. ' MsgBox (timeRange)
  122. If (time <> timeLast And IsInArray(CInt(time), flipArr)) And Count > 2 Or timeRange > 4 Then
  123. colorFlip = Not colorFlip
  124. timeFirst = time
  125. ThisWorkbook.Worksheets(x).Cells(Count - 1, 11) = flipCount
  126. ThisWorkbook.Worksheets(x).Cells(Count - 1, 11).Interior.ColorIndex = ThisWorkbook.Worksheets(x).Cells(Count - 1, 1).Interior.ColorIndex
  127. If flipCount > maxCalls Then
  128. maxCalls = flipCount
  129. maxCallsRow = Count - 1
  130. ' MsgBox (CStr(maxCalls) & " " & CStr(x))
  131. ' MsgBox (maxCallsRow)
  132. Else
  133. End If
  134.  
  135. flipCount = 1
  136.  
  137. Else
  138. flipCount = flipCount + 1
  139. End If
  140.  
  141. ' Set Colors
  142. If colorFlip = True Then
  143. ThisWorkbook.Worksheets(x).Cells(Count, 1).Interior.ColorIndex = 17
  144. Else
  145. ThisWorkbook.Worksheets(x).Cells(Count, 1).Interior.ColorIndex = 37
  146. End If
  147.  
  148. Count = Count + 1
  149.  
  150.  
  151.  
  152. Wend
  153.  
  154. ThisWorkbook.Worksheets(3).Cells(startRow + 1, 8) = "Max Calls Interval:" ' & ": " & CStr(maxCalls)
  155. maxRange = ThisWorkbook.Worksheets(x).Range("G" & CStr(maxCallsRow - maxCalls + 1) & ":" & "G" & CStr(maxCallsRow))
  156. ThisWorkbook.Worksheets(3).Cells(startRow + 2, 10) = WorksheetFunction.Sum(maxRange) / maxCalls
  157. ThisWorkbook.Worksheets(3).Cells(startRow + 2, 10).NumberFormat = "0.00"
  158. ThisWorkbook.Worksheets(3).Cells(startRow + 2, 11) = WorksheetFunction.Max(maxRange)
  159. maxRange = ThisWorkbook.Worksheets(x).Range("G" & CStr(maxCallsRow - maxCalls + 1) & ":" & "H" & CStr(maxCallsRow))
  160. ThisWorkbook.Worksheets(3).Cells(startRow + 2, 12) = WorksheetFunction.Sum(maxRange) / maxCalls
  161. ThisWorkbook.Worksheets(3).Cells(startRow + 2, 12).NumberFormat = "0.00"
  162. ThisWorkbook.Worksheets(3).Cells(startRow + 2, 8) = Mid(ThisWorkbook.Worksheets(x).Cells(maxCallsRow - maxCalls + 1, 1), 12, 5) & " To " & Mid(ThisWorkbook.Worksheets(x).Cells(maxCallsRow + 1, 1), 12, 5)
  163. ThisWorkbook.Worksheets(3).Cells(startRow + 2, 13) = maxCalls
  164.  
  165. startRow = startRow + 7
  166.  
  167. Next x
  168.  
  169. ThisWorkbook.Worksheets(3).Columns("H:N").AutoFit ' autofit columns
  170. ThisWorkbook.Worksheets(3).Columns("I").ColumnWidth = 1
  171. ThisWorkbook.Worksheets(3).Columns("J:N").HorizontalAlignment = xlCenter
  172.  
  173. starting_ws.Activate 'activate the worksheet that was originally active
  174.  
  175. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement