excel:获取日期并添加一天

时间:2017-07-10 13:14:24

标签: excel vba excel-vba

我有这个简单的电子表格,上面有名字和几个日期。

我想要发生的是,如果某些条件属实,我将获得第一张纸的结束日期(第H列)并将其复制到我的第二张纸(G栏),并将更多日期添加到它

这是第一张名为BASE_TOTAL的表格。

enter image description here

这是第二个,名为APOIOS

enter image description here

" sss"代表我想要输入的日期加上一天。

这是我迄今为止所尝试过的:

Sub Apoios()

Dim i As Long
Dim j As Long
Dim lastrow As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim res

Set ws1 = Sheets("Plan3")
Set ws2 = Sheets("BASE_TOTAL")
Set ws3 = Sheets("APOIOS")

lastrow = ws2.Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow
    For j = 1 To 11
        Select Case True
            Case IsEmpty(ws2.Cells(i, j)):
                ws3.Cells(i, j) = ""

            Case Not IsEmpty(ws2.Cells(i, j)):
                If ws1.Cells(4, 3) <= ws2.Cells(i, 8) And ws2.Cells(i, 5).Value <> "APOIO" Then
                    ws3.Cells(i, 1).Value = ws2.Cells(i, 1)
                    ws3.Cells(i, 2).Value = ws2.Cells(i, 2)
                    ws3.Cells(i, 3).Value = ws2.Cells(i, 3)
                    ws3.Cells(i, 4).Value = ws2.Cells(i, 4)
                    ws3.Cells(i, 5).Value = "APOIO"
                    ws3.Cells(i, 6).Value = "-"
                    ws3.Cells(i, 7).Value = ws2.Cells(i, 8).Value + 1 
                    ws3.Cells(i, 8).Value = "-------------"
                    ws3.Cells(i, 9).Value = ws2.Cells(i, 9)
                    ws3.Cells(i, 10).Value = ws2.Cells(i, 10)
                    ws3.Cells(i, 11).Value = ws2.Cells(i, 11)
                End If

        End Select
    Next j
Next i

End Sub

正如您所看到的,我试图用这行代码来做到这一点:

ws3.Cells(i, 7).Value = ws3.Cells(i, 8).Value + 1

但它没有成功。我是VBA的新手,所以我不知道应该使用哪种功能来理解我希望在该单元格上添加日期并再添加一天。

我知道存在DateSerial功能,但我不知道如何应用此示例。我也找到THIS回答,我会检查它是否有效。 虽然我的日期是dd/mm/yyyy格式。

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您是否尝试过DateAdd功能? 像

ws3.Cells(i, 7).Value = DateAdd("d", 1, ws2.Cells(i, 8).Value)