显示excel中2个日期之间的年份

时间:2016-03-14 14:15:54

标签: excel excel-formula

我在excel中有一个包含2个日期的列:

01.03.2006 - 01.11.2011

是否有可能在另外一栏中写下这两个日期之间的年份?我应该得到2006,2007,2008,2009.2010,2011。多年之间的分隔符并不重要。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

我相信有人可以用公式计算出来,但这是一个粗略的VB解决方案:

Function return_Missing(ByVal firstDate As Range, ByVal lastDate As Range)
Dim i&, k&
k = 1

For i = firstDate To lastDate - 1
    return_Missing = return_Missing & firstDate + k & ", "
    k = k + 1
Next i

return_Missing = Left(Trim(return_Missing), Len(Trim(return_Missing)) - 1)

Debug.Print return_Missing
End 

答案 1 :(得分:0)

这样的事情,我想:

Public Function YearsBetweenDates(dateRange As String) As Variant
On Error GoTo ErrorHandler
    Dim splitResult() As String
    Dim year1, year2 As Integer
    Dim result As String
    splitResult = Split(Replace(dateRange, " ", ""), "-")
    year1 = Year(CDate(splitResult(0)))
    year2 = Year(CDate(splitResult(1)))
    If year1 <= year2 Then
        result = CStr(year1)
        Do While year1 < year2
            year1 = year1 + 1
            result = result & "," & CStr(year1)
        Loop
    Else
        GoTo ErrorHandler
    End If
    YearsBetweenDates = result
    Exit Function
ErrorHandler:
    YearsBetweenDates = xlErrValue
End Function