[英]calculate with month, part of months
我需要的是計算一個月內的可用性(WTF),並將其除以所選月份中的每月天數。
公式應為:WTF /一個月的總天數*一個月的可用天數=結果
例如:
period: 07-09-2017 - 15-11-2017
WTF: 0.5
應導致:
September: 0,4000
October: 0,5000
November: 0,2500
我的問題是,當此期間超過2個月時,如何計算結果。 我無法弄清楚如何計算第一個月和上個月之間的月份的結果,因此在這種情況下為十月份。
這是一些入門的代碼。 它以一個字符串輸出值,但是您可以輕松地將其輸出到三個不同的單元格:
Public Function OutputWork(first As Date, last As Date, wtf As Double)
Dim worked As Object, total As Object
Dim i As Date
Dim j As Long
Dim mnth As String, key As String
Dim v As Variant
Set worked = CreateObject("Scripting.Dictionary")
Set total = CreateObject("Scripting.Dictionary")
For i = WorksheetFunction.EoMonth(first, -1) + 1 To WorksheetFunction.EoMonth(last, 0)
mnth = Format(i, "mmmm")
If Not total.exists(mnth) Then
total.Add key:=mnth, Item:=1
Else: total.Item(mnth) = total.Item(mnth) + 1
End If
If Not worked.exists(mnth) Then worked.Add key:=mnth, Item:=0
If i >= first And i <= last Then worked.Item(mnth) = worked.Item(mnth) + 1
Next i
ReDim v(LBound(total.keys()) To UBound(total.keys()))
For j = LBound(v) To UBound(v)
key = total.keys()(j)
v(j) = key & ":" & wtf / total.Item(key) * worked.Item(key)
Next j
OutputWork = Join(v, ", ")
End Function
使用工作表上的函數,如下所示:
=OutputWork(<start>,<end>,<WTF>)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.