Вы хотите использовать пользовательскую функцию на листе, но в вашей пользовательской функции есть содержимое, которое изменяет значение ячейки. Это невозможно сделать, применяя функцию непосредственно к листу, поэтому возникает ошибка.
'Debugging printing
tempRange.Cells(1 + n, 10) = weekNext
tempRange.Cells(1 + n, 12) = serviceWorkerIDNext
tempRange.Cells(1 + n, 14) = ClientIDNext
Использование вашей функции внутри процедуры работает, как показано ниже.
Sub test()
Dim c As Integer
c = clientOrServiceWorkerCount(2, 3, 1, 1, "Sheet2")
Range("r1") = c
End Sub
Если вы закомментируете свой проблемный код, результат хорошо работает на листе.
Public Function clientOrServiceWorkerCount( _
startWeek As Integer, _
endWeek As Integer, _
searchID As Long, _
outputType As Integer, _
sheetName As Variant) As Integer
clientOrServiceWorkerCount = 0
Dim WSMod111 As Worksheet
Set WSMod111 = Sheets(sheetName)'<~~ Your code should change like this.
Dim rowOffset As Integer
rowOffset = 3
Dim endIndex As Integer
endIndex = WSMod111.Cells(WSMod111.Rows.Count, 13).End(xlUp).Row - rowOffset
Dim tempRange As Range
Set tempRange = WSMod111.Range("U4:Z" & endIndex)
Dim tempArray() As Integer
Dim weekNow As Integer
Dim weekNext As Integer
Dim ClientIDNow As Long
Dim ClientIDNext As Long
Dim serviceWorkerIDNow As Integer
Dim serviceWorkerIDNext As Integer
Dim arrayID As Integer
Dim serviceWorkerCount As Integer
Dim cLCount As Integer
Dim colOffset As Integer
Dim arrayCount As Integer
arrayCount = 0
' Offset which columns to refer to based on the outputType value. 1 = search for client then service workers
' 2 = search for service workers and then clients
If outputType = 1 Then
colOffset = 0
End If
If outputType = 2 Then
colOffset = 3
End If
'Build the array for the week range of interest, defined by startWeek and endWeek
For i = 0 To endIndex - 1
weekNow = tempRange(i + 1, 1 + colOffset)
arrayID = tempRange(i + 1, 2 + colOffset)
If weekNow >= startWeek And weekNow <= endWeek And arrayID = searchID Then
ReDim Preserve tempArray(arrayCount)
tempArray(arrayCount) = i + 1
arrayCount = arrayCount + 1
'Print the results to the worksheet to check answers
'tempRange.Cells(arrayCount, 8) = tempArray(arrayCount - 1)
End If
Next i
For n = 0 To arrayCount - 1
weekNow = tempRange(tempArray(n), 1 + colOffset)
serviceWorkerIDNow = tempRange(tempArray(n), 2 + colOffset)
ClientIDNow = tempRange(tempArray(n), 3 + colOffset)
'Debugging printing
'tempRange.Cells(1 + n, 9) = weekNow
'tempRange.Cells(1 + n, 11) = serviceWorkerIDNow
'tempRange.Cells(1 + n, 13) = ClientIDNow
If n < arrayCount - 1 Then
'weekNext = tempRange(tempArray(n + 1), 1 + colOffset)
'serviceWorkerIDNext = tempRange(tempArray(n + 1), 2 + colOffset)
'ClientIDNext = tempRange(tempArray(n + 1), 3 + colOffset)
'Debugging printing
'tempRange.Cells(1 + n, 10) = weekNext
'tempRange.Cells(1 + n, 12) = serviceWorkerIDNext
'tempRange.Cells(1 + n, 14) = ClientIDNext
End If
If outputType = 1 Then
If ClientIDNow <> ClientIDNext Or n = arrayCount - 1 Then
cLCount = cLCount + 1
End If
End If
If outputType = 2 Then
If serviceWorkerIDNow <> serviceWorkerIDNext Or n = arrayCount - 1 Then
serviceWorkerCount = serviceWorkerCount + 1
End If
End If
Next
'Return the count of either the clients (outputType=1) or the serviceWorkerinators (outputType=2)
If outputType = 1 Then
clientOrServiceWorkerCount = cLCount
End If
If outputType = 2 Then
clientOrServiceWorkerCount = serviceWorkerCount
End If
End Function