数组月末的一个公式只有2个日期

时间:2013-07-25 04:52:26

标签: excel google-sheets

A1: I have 1/1/13
A2: I have 7/25/13

如何在A3中使用一个公式,以便输出如下所示:

1/31/13,2/28/13,3/31/13,4/30/13,5/31/13,6/30/13,7/31,13

即。在一个单元格中,一个公式为我提供了所有月末日期。

请不要代码。

1 个答案:

答案 0 :(得分:4)

把它放在一个模块中:

Function myFormula(startDate, endDate)
    On Error GoTo err
    Dim result As String
    result = ""
    noOfMonths = DateDiff("m", startDate, endDate)
    For i = 1 To noOfMonths + 1
        Dim myDate As Date
        myDate = DateAdd("d", Day(startDate) - 1, startDate)
        myDate = DateAdd("m", i, myDate)
        myDate = DateAdd("d", -1, myDate)
        result = result & Format(myDate, "m\/d\/yy")
        If i <= noOfMonths Then result = result & ","
    Next i
    myFormula = result
ex:
    Exit Function
err:
    MsgBox (err.Number & err.Description)
    myFormula = "Error"
    Resume ex
End Function

,这在A3中: = myFormula(A1; A2)