0
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

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

请没有代码。

4

1 回答 1

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)

于 2013-07-25T08:13:49.037 回答