gpt4 book ai didi

vba - 用月份计算,部分月份

转载 作者:行者123 更新时间:2023-12-04 21:32:14 24 4
gpt4 key购买 nike

我需要的是计算一个月的可用性(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>)

enter image description here

关于vba - 用月份计算,部分月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253659/

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