Vba 每月两个日期之间的总天数
有人建议VBA函数也这样做吗?以数组格式返回将非常有用,这样我可以在其他计算中使用它。这是通过VBA进行现金流计算所必需的 开始日期-2015年1月1日 截止日期-2015年3月5日 VBA结果Vba 每月两个日期之间的总天数,vba,excel,Vba,Excel,有人建议VBA函数也这样做吗?以数组格式返回将非常有用,这样我可以在其他计算中使用它。这是通过VBA进行现金流计算所必需的 开始日期-2015年1月1日 截止日期-2015年3月5日 VBA结果 Count Month Month Days 1 1/Jan/2015 31 2 1/Feb/2015 28 3 1/Mar/2015 5 注意-如果提供的日期错误,那么VBA必须假定默认日期
Count Month Month Days
1 1/Jan/2015 31
2 1/Feb/2015 28
3 1/Mar/2015 5
注意-如果提供的日期错误,那么VBA必须假定默认日期类似于这样
Function GetTable(startDate As Double, endDate As Double) As Variant
Dim Table() As Variant, i As Long, y As Byte: i = 1: y = Day(startDate)
If endDate <= startDate Then GetTable = "error": Exit Function
ReDim Table(2, 0)
Table(0, 0) = "Count"
Table(1, 0) = "Month"
Table(2, 0) = "Month Days"
For startDate = startDate To endDate - 1
If Month(startDate + 1) <> Month(startDate) Then
ReDim Preserve Table(2, UBound(Table, 2) + 1)
Table(0, UBound(Table, 2)) = UBound(Table, 2)
If UBound(Table, 2) = 1 Then
Table(1, UBound(Table, 2)) = y & Format(startDate, "/mmm/yyyy")
Else
Table(1, UBound(Table, 2)) = Format(startDate, "1/mmm/yyyy")
End If
Table(2, UBound(Table, 2)) = i
i = 1
Else
i = i + 1
End If
Next
ReDim Preserve Table(2, UBound(Table, 2) + 1)
Table(0, UBound(Table, 2)) = UBound(Table, 2)
If UBound(Table, 2) = 1 Then
Table(1, UBound(Table, 2)) = y & Format(startDate, "/mmm/yyyy")
Else
Table(1, UBound(Table, 2)) = Format(startDate, "1/mmm/yyyy")
End If
Table(2, UBound(Table, 2)) = i
GetTable = Table
End Function
下面是另一种方法: 用于获取一个月内天数的函数:
' https://msdn.microsoft.com/en-us/library/aa227538(v=vs.60).aspx
Function dhDaysInMonth(Optional dtmDate As Date = 0) As Integer
If dtmDate = 0 Then
dtmDate = Date
End If
dhDaysInMonth = DateSerial(Year(dtmDate), _
Month(dtmDate) + 1, 1) - _
DateSerial(Year(dtmDate), Month(dtmDate), 1)
End Function
函数使用所需信息填充数组
Function GetDateArray(StartDate As Date, EndDate As Date)
Dim Holder() As Variant
Dim i As Date, Count As Integer, temp As Integer
my = Format(StartDate, "mm/yyyy")
Count = 0
' pass 1 - find out how many months we encountered
' set up the array bounds accordingly
For i = StartDate To EndDate
' each time month/year combination changes, we increment our count
If Format(i, "mm/yyyy") <> my Then
Count = Count + 1
my = Format(i, "mm/yyyy")
End If
Next
ReDim Holder(1 To Count + 1, 1 To 3)
my = Format(StartDate, "mm/yyyy")
Count = 0
' pass 2 - populate the array with information
For i = StartDate To EndDate
If Format(i, "mm/yyyy") <> my Then
Count = Count + 1
' find days in the month
temp = dhDaysInMonth(i - 1)
If Count = 1 Then
temp = temp - Format(StartDate, "dd") + 1
End If
' populate array
Holder(Count, 1) = Count
Holder(Count, 2) = "01" & "/" & Format(i - 1, "mmm/yyyy")
Holder(Count, 3) = temp
' reset mm/yyyy we remembered
my = Format(i, "mm/yyyy")
End If
Next
' handle the last month's information
Count = Count + 1
temp = Format(EndDate, "dd")
Holder(Count, 1) = Count
Holder(Count, 2) = "01" & "/" & Format(i - 1, "mmm/yyyy")
Holder(Count, 3) = temp
GetDateArray = Holder
End Function
2015年2月27日至5月5日期间的结果:
1 | 01/Feb/2015 | 2 |
2 | 01/Mar/2015 | 31 |
3 | 01/Apr/2015 | 30 |
4 | 01/May/2015 | 5 |
谢谢…但当我运行上述功能时,我得到的结果如下:计数月份天数1 30/1/2015 31日期显示为2015年1月30日,而不是2015年1月1日2 1/2/2015 28 3 1/3 1/3/2015 0,而不是5将单元格转换为文本,然后再次运行。。。excel自动格式化它。。。还有一个错误。。。我一回家就解决它。。。也可以使用转置,否则将被翻转。redim PREVENT仅适用于最后一个维度。如果第一天是1月30日,则它将正确显示为1月30日。。。然而:如果你希望它总是1/月/年,只要说出来。。。
1 | 01/Feb/2015 | 2 |
2 | 01/Mar/2015 | 31 |
3 | 01/Apr/2015 | 30 |
4 | 01/May/2015 | 5 |