Option Explicit
'Look Up Name, Find And Return Total Of Selected Option'
Public Function GETSHRINKAGE(Name As String, Activity As String, Report As Range) As Variant
'-----------------'
'Declare Variables'
'-----------------'
Dim StartRowIndex As Long 'To Store The First Row Number'
Dim EndRowIndex As Long 'To Store The Last Row Number'
Dim ReturnValue As Date 'To Hold The Running Total'
Dim TestRange As Range 'To Hold The Resulting Range Of The Search'
'-------------'
'Set Variables'
'-------------'
ReturnValue = TimeValue("00:00:00") 'Set Starting Value So 00:00:00 Is Returned For No Match'
StartRowIndex = 0 'Set Initial Value'
EndRowIndex = 0 'Set Initial Value'
'--------------------'
'Get Start Row Number'
'--------------------'
Set TestRange = Report.Find(Name) 'Look For Name And Store Resulting Range'
'Test If Name Found'
If TestRange Is Nothing Then
GETSHRINKAGE = ReturnValue
Exit Function
End If
'Result Found/Store Start Row Index'
StartRowIndex = TestRange.Row
'--------------------'
'Get End Row Number'
'--------------------'
Dim LoopCounter As Long
LoopCounter = StartRowIndex + 1
'Loop Rows Until Next Name Found'
Do Until Left(Report.Range("A" & LoopCounter).Text, 5) = "Agent"
LoopCounter = LoopCounter + 1
If LoopCounter > StartRowIndex + 500 Then Exit Do
Loop
EndRowIndex = LoopCounter 'Set End Row Index'
'---------------------------------------------'
'Loop Rows And Look For Option And Add Up Time'
'---------------------------------------------'
Dim Cell As Range
For Each Cell In Report.Range("B" & StartRowIndex, "B" & EndRowIndex)
If Cell.Text = Activity Then
ReturnValue = ReturnValue + TimeValue(Cell.Offset(0, 1).Text)
End If
Next Cell
'-------------'
'Return Result'
'-------------'
GETSHRINKAGE = ReturnValue
End Function