Option Explicit
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim lastRow As Long, Work, Labels
Dim arrList(), i As Long
Dim cntDict As Object, sumDict As Object, key As Variant
' Label1~Label4 is in same order as Work
Work = Array("Carpenter", "Painter", "Dancer", "Singer")
Labels = Array("lab_Carpenter", "lab_Painter", "lab_Dancer", "lab_Singer")
Set cntDict = CreateObject("Scripting.Dictionary") 'Initialize Dictionary
Set sumDict = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Worksheets("Sheet2")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow
key = ws.Cells(i, 3).Value
If Not cntDict.Exists(key) Then
cntDict.Add key, 1
sumDict.Add key, ws.Cells(i, 6).Value
Else
cntDict(key) = cntDict(key) + 1
sumDict(key) = sumDict(key) + ws.Cells(i, 6).Value
End If
Next i
ReDim arrList(cntDict.Count, 1 To 3)
arrList(0, 1) = "Work"
arrList(0, 2) = "Total_Hours"
arrList(0, 3) = "Average"
For i = 0 To UBound(Work)
key = Work(i)
arrList(i + 1, 1) = key
arrList(i + 1, 2) = Format(sumDict(key), "h:mm:ss")
arrList(i + 1, 3) = Format(sumDict(key) / cntDict(key), "h:mm:ss")
' Predefined lables name with array
Me.Controls(Labels(i)).Caption = cntDict(key)
' If label controls name follow a pattern
' Me.Controls("lab_" & key).Caption = cntDict(key)
Next
With Me.ListBox2
.ColumnCount = 3
.List = arrList
End With
End Sub