一列日期的DateDiff

时间:2017-05-25 10:45:11

标签: excel vba datediff

我可以帮助解决这个问题,我正在寻找一个列中日期的日期差异,如下所示,

enter image description here

显然我已经尝试了for循环,但它从来没有那么容易,任何人都可以帮助:)

2 个答案:

答案 0 :(得分:1)

如果您的日期全部位于列A中的B A3-A2列,并向下拖动所有具有日期的单元格(除了最后一个)

enter image description here

答案 1 :(得分:0)

如果您尝试使用VBA实现此目的,假设您的日期在A2:A6范围内,尝试这样的事情......

Sub DateDif()
Dim lr As Long, i As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lr - 1
    If IsDate(Cells(i, 1)) And IsDate(Cells(i + 1, 1)) Then
        Cells(i, 2) = DateDiff("d", Cells(i, 1), Cells(i + 1, 1))
    End If
Next i
End Sub

或者尝试这样容易调整......

Sub DateDif()
Dim lr As Long
Dim Rng As Range, Cell As Range
lr = Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = Range("B2:B" & lr)
For Each Cell In Rng
    If IsDate(Cell) And IsDate(Cell.Offset(1, 0)) Then
        Cell.Offset(0, 1) = DateDiff("d", Cell, Cell.Offset(1, 0))
    End If
Next Cell
End Sub