在选择中更改日期

时间:2016-08-10 10:40:59

标签: excel vba

我使用宏来更改所选范围内的日期。它工作正常,但在选择范围内时,有一个非日期值的单元格(例如" abcdef"或单元格为空)我运行宏它将这个非日期值转换为日期。我该如何解决?

当前代码:

Sub adddate()
Dim cell As Range
Dim r As Range
Set r = Selection

For Each cell In Selection
If IsDate(cell.Value) Then
Selection.Cells = DateAdd("d", 28, CDate(cell))
End If
Next cell

End Sub

1 个答案:

答案 0 :(得分:0)

Sub adddate()
    Dim cell As Range

    For Each cell In Selection
        If IsDate(cell.Value) Then cell = DateAdd("d", 28, CDate(cell))
    Next cell
End Sub