我有一个工作项目,我正在尝试计算员工工作的总天数,其中一个工作表中的数据输入同一工作表中计算的天数,或者同一工作簿中的另一个工作表中的数据。
如果数据被隔离到一个员工,那么我可以使用:
=SUM(IF(FREQUENCY(B:B,B:B)>0,1))
计算工作天数。我已经尝试使用其他几个公式来获得我要查找的值,包括:
=SUMPRODUCT((TEXT('Data'!$A$1:$A$100, "yyyymm")="201804")*('Data'!B$1:$B$100="John Doe"))
和
=SUMIFS(IF(FREQUENCY('Sono Detail'!$B:$B,'Sono Detail'!$B:$B)>0,1),'Sono Detail'!$E:$E,'(Test) Sono Report Card 1.0'!$B$3)
这两种方法都不起作用。
发布于 2018-04-26 20:10:24
这还不是一个代码解决方案,但我发布这个答案是为了说明您可以根据您提供的示例从数据透视表中派生出哪些数据。
在第一个字段排列中,您可以看到透视图显示了每天为每个员工执行了多少个程序。这包括时间段内每个员工的程序总数。因此,“员工1”在3月1日执行了14个程序,以此类推。
如果我更改了pivot的Values字段,我可以显示每个员工执行程序所花费的总时间(以DAYS为单位)。因此,在下表中,员工1在3月1日工作了0.15833天。这相当于288分钟(或3.8小时)。
然后,遍历数据透视表的VBA代码将标识每个员工以及他们工作的天数,并且还可以访问其他汇总数据。
发布于 2018-04-26 20:48:10
如果可以使用VBA宏,请尝试执行以下操作:
在单元格L3类型中:
=WorkingDaysCount(B:B,C:C,K3)
对L4、L5和L6中的其他员工执行类似的操作。
然后在新的代码模块中使用下面的代码
Option Explicit
Public Function WorkingDaysCount(ByRef Dates As Range, ByRef EmployeeRange As Range, ByVal Employee As String) As Integer
Dim ct As Integer 'counter
Dim i As Long
'for looping
Dim EmployeeDates() As Date 'array to track which dates are already counted
ReDim EmployeeDates(0) 'create initial
'loop through all the cells
For i = 1 To Dates.Count
If (EmployeeRange(i) = Employee) Then 'if the employee is equal to the employee in that range
If (Not (IsInArray(Dates(i), EmployeeDates))) Then 'if the date hasn't already been counted
EmployeeDates(UBound(EmployeeDates)) = Dates(i) 'add the date
ReDim Preserve EmployeeDates(UBound(EmployeeDates) + 1) 'add another element to the array for the next one
End If
End If
Next i
WorkingDaysCount = UBound(EmployeeDates) 'since ubound is 0-based this is the total count of dates
End Function
Private Function IsInArray(valToBeFound As Variant, arr As Variant) As Boolean
'DEVELOPER: Ryan Wells (wellsr.com)
'DESCRIPTION: Function to check if a value is in an array of values
'INPUT: Pass the function a value to search for and an array of values of any data type.
'OUTPUT: True if is in array, false otherwise
Dim element As Variant
On Error GoTo IsInArrayError: 'array is empty
For Each element In arr
If element = valToBeFound Then
IsInArray = True
Exit Function
End If
Next element
Exit Function
IsInArrayError:
On Error GoTo 0
IsInArray = False
End Function
https://stackoverflow.com/questions/50049107
复制