2015-11-05 232 views
0

有人建議使用VBA功能嗎?以數組格式返回會很有用,所以我可以在其他計算中使用它。這是通過VBA進行現金流量計算所需的。兩個日期之間的總天數

開始日期 - 01 /月/ 2015年
結束日期 - 05 /三月/ 2015

VBA結果

Count  Month  Month Days 
1   1/Jan/2015 31 
2   1/Feb/2015 28 
3   1/Mar/2015  5 

注意 - 如果提供的日期是錯誤的,那麼VBA不得不假設默認日期

回答

0

是這樣的?

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 
+0

謝謝....但是當我運行上面的功能我得到的結果如下 計數逐月天 1 30/2015年1月31日(日期顯示爲2015年1月30日而非2015年1月1日) 2 1/Feb/2015 28 3 1/Mar/2015 0(而不是5) – cooolboy

+0

將您的單元格轉換爲文本然後再次運行。 .. excel autoformates它...仍然...有一個錯誤...虐待儘快解決它,即時通訊家...也使用轉置或它會被翻轉(redim只保留作品與最後一個維度) –

+0

另外,如果第一天是30月1日,它會正確地顯示它作爲30日...但是:如果你想要它總是1 /月/年,只是說... –

0

這裏的另一種方式,你可以這樣做:

功能得到天#一個月:

' 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 

測試以確保函數返回我們需要的東西

Sub test() 

    ' get data you desire 
    Dim Holder() As Variant 
    Holder = GetDateArray("2/27/2015", "5/5/2015") 

    ' debug/print the array just as a proof 
    Dim Row As Integer, Col As Integer, TempStr As String 
    For Row = 1 To UBound(Holder, 1) 
     TempStr = "" 
     For Col = 1 To UBound(Holder, 2) 
      TempStr = TempStr & Holder(Row, Col) & " | " 
     Next 
     Debug.Print TempStr 
    Next 

End Sub 

結果(與2月27日和2015年5月5日):

1 | 01/Feb/2015 | 2 | 
2 | 01/Mar/2015 | 31 | 
3 | 01/Apr/2015 | 30 | 
4 | 01/May/2015 | 5 |