将一天添加到行中所有单元格中的日期字段 - VBA

时间:2017-11-02 04:29:43

标签: excel vba excel-vba date add

我在D1单元格中有一个日期(2/1/2018),现在我想使用VBA将所有单元格中的日期从D2增加到AH

有什么建议吗?

感谢 SREE

1 个答案:

答案 0 :(得分:0)

请注意,如果可变数据类型设置为Date,则在与1求和时将增加日期。

VBA:

Sub DateIncreaser

    Dim d as Date
    d = Selection.value 'Assign appropriated value to d variable.
    d = d + 1
    debug.print d 'This optional line shows the result in immediate window.
    Range("D2").Value = d

End Sub

UPDATE1

Sub DateIncreaser()

    Dim d As Date
    Dim i As Integer
    Dim InitialColumnIndex As Integer
    Dim FinalColumnIndex As Integer
    Dim RowsIndex As Long

    InitialColumnIndex = 1
    FinalColumnIndex = 34 'Representative AH Column
    RowsIndex = 2
    d = Selection.Value 'Source date value

    For i = InitialColumnIndex To FinalColumnIndex
        d = d + 1
        Cells(RowsIndex, i).Value = d
    Next i

End Sub

UPDATE2

Sub DateIncreaser()

    Dim d As Date
    Dim i, j As Integer
    Dim InitialColumnIndex As Integer
    Dim FinalColumnIndex As Integer
    Dim RowsIndex As Long

    InitialColumnIndex = 1
    FinalColumnIndex = 34 'Representative AH Column
    RowsIndex = 2

    For j = 1 To Sheets.Count

        d = Range("D1").Value 'Source date value

        For i = InitialColumnIndex To FinalColumnIndex
            d = d + 1
            Worksheets(j).Cells(RowsIndex, i).Value = d
        Next i

    Next j

End Sub