我有一个工作项目,我正在尝试计算员工工作的总天数,其中一张表中的数据提供了在同一张表上计算的数字,或同一工作簿中的另一张表。
每个工作日都有几行数据,因此我希望分别计算每位员工的唯一日期总数。如果数据被隔离给一名员工,那么我可以使用:
=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)
这些都没有奏效。
答案 0 :(得分:0)
这不是一个代码解决方案(但是),但是我发布这个答案来说明您可以根据您提供的示例从数据透视表中获得的数据。
在第一个字段排列中,您可以看到数据透视表显示每位员工每天执行的程序数。这包括该时间段内每位员工的总程序数。所以"员工1"在3月1日进行了14个程序,依此类推。
如果我更改了数据透视的值字段,我可以显示每个员工执行程序所花费的总时间( DAYS )。因此,在下表中,员工1在1月1日工作了0.15833天。这相当于288分钟(或3.8小时)。
遍历数据透视表的VBA代码将识别每个员工以及他们工作了多少天,并且还可以访问其他总结数据。
答案 1 :(得分:0)
如果您可以使用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