Add this to the form:
Private Sub ComboBox1_Change()
Dim lngRow As Long
Dim rngData As Range
If Me.ComboBox1.ListIndex <> -1 Then
' get row number - employee range starts in row 6
lngRow = 6 + Me.ComboBox1.ListIndex
With Sheet1
Set rngData = .Range(.Cells(lngRow, "C"), .Cells(lngRow, "NC"))
End With
Me.TextBox1.Value = CountByColor(rngData, Sheet1.Range("B22")) ' holiday
Me.TextBox2.Value = CountByColor(rngData, Sheet1.Range("B23")) ' sick leave
Me.TextBox3.Value = CountByColor(rngData, Sheet1.Range("B24")) ' other
End If
End Sub
Function CountByColor(InputRange As Range, ColorRange As Range) As Long
Dim cl As Range, TempCount As Long, ColorIndex As Integer
ColorIndex = ColorRange.Cells(1, 1).Interior.ColorIndex
TempCount = 0
For Each cl In InputRange.Cells
If cl.Interior.ColorIndex = ColorIndex Then
TempCount = TempCount + 1
End If
Next cl
Set cl = Nothing
CountByColor = TempCount
End Function
Bookmarks