gpt4 book ai didi

excel - 查找在连续数据中花费的时间

转载 作者:行者123 更新时间:2023-12-04 22:18:21 25 4
gpt4 key购买 nike

我正在寻找在数据集中花费的总时间
我的数据集可能如下所示:
(第一次建议后更新数据集)


起始时间
结束时间


44224,32869
44224,33603

44224,30975
44224,33616

44224,30965
44224,32824

44223,34859
44223,46875

44223,41349
44223,44875


这将给我 9000 - 4000,50 - 500 的总时间(从没有时间在 5000 和 5500 之间工作)= 4499,5
找到最小开始时间和最大结束时间给了我这个集合的范围,如果下一个开始时间大于上一个结束时间(例如在 5000 到 6000 中,减去 1000),我可以从中减去数据。但是,从最后一个数据点来看,这 1000 个减去的时间单位中有 500 个已用于总时间。有什么简单的方法可以从这样的数据集中找到总花费的时间吗?在 VBA Excel 中编程。谢谢你的时间!
当前使用此代码:

    For i = 7 to lastRow

If timeEnd(i) < timeStart(i - 1) Then

subtractTime = subtractTime + (timeStart(i - 1) - timeEnd(i))

End If

If timeStart(i) < firstTime Then

firstTime = timeStart(i)

End If


If timeEnd(i) > lastTime Then

lastTime = timeEnd(i)

End If

totalTimeSpent(i) = lastTime - firstTime - subtractTime

Next i


找到了一个适用于我的数据集的解决方案:首先对数据进行排序,使其按顺序排列,然后运行上面的代码。

最佳答案

这是很长的路要走,它将时间从最低到最高循环,并在考虑时进行计数:

    Function mytime(stRng As Range, edRng As Range)
Dim stArr() As Variant
stArr = Intersect(stRng.Parent.UsedRange, stRng).Value

Dim edArr() As Variant
edArr = Intersect(edRng.Parent.UsedRange, edRng).Value
If UBound(stArr, 1) <> UBound(edArr, 1) Then Exit Function

Dim cnt As Long
cnt = 0

For i = Application.Min(stArr) To Application.Max(edArr)
For j = 1 To UBound(stArr, 1)
If i >= stArr(j, 1) And i < edArr(j, 1) Then
cnt = cnt + 1
Exit For
End If
Next j
Next i

mytime = cnt

End Function
enter image description here

关于excel - 查找在连续数据中花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66693871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com