[英]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.