excel将日期添加到日期,然后使用它自动填充另一个工作表中的单元格

时间:2016-06-14 13:07:23

标签: excel vba excel-vba date

如标题中所述我需要另一张表格中的单元格来自动填充新日期,该日期已添加到当前日期,这是我无法工作的代码:

Sub dateplus()

Dim destSheet As Worksheet: Set destSheet = ThisWorkbook.Worksheets("Sheet1")
Dim Ldate As Date

Ldate = dateadd("d", 2, "6/14/2016")

destSheet.range("A1").Value = Ldate

End Sub

我对dateadd的这个命令并不完全熟悉,为了让这段代码能够完成我想要完成的任务,我需要更改什么?

谢谢

3 个答案:

答案 0 :(得分:1)

    ,update_setup_time: function(e) {
    var d = $('#setup_form_popup').val();
    $.ajax({
        url: '/events/update_setup_time_p',
        type: 'POST',
        dataType: 'json',
        data: d,
        success: function() {
            alert(d + "how are you doing?");
        }
    });
}

答案 1 :(得分:1)

您的destSheet似乎尚未初始化,而sourceSheet则为Sub CustomDateAdd() Dim destSheet As Worksheet Dim Ldate As Date Set destSheet = ThisWorkbook.Worksheets("Sheet1") Ldate = DateAdd("d", 2, Now) destSheet.range("A1").Value = Format(Ldate,"MM/dd/yyyy") End Sub 。要使用DateAdd,您需要一个Date对象,而不是一个字符串。它应该是这样的:

Sub CustomDateAdd()

Dim destSheet As Worksheet
Set destSheet = ThisWorkbook.Worksheets("Sheet1")

destSheet.range("A1").Formula = "=TODAY()+2"

End Sub

为什么不使用Excel公式而不是手动计算?

  $array = array(
    '6' => array(
      '945' => array(
        '11:04' => array(
          '0' => array(
            'date' => '2016-03-14 11:38:01'
          ),
          '1' => array(
            'date' => '2016-03-14 11:38:39'
          ),
          '2' => array(
            'date' => '2016-03-14 11:38:43'
          ),
          '3' => array(
            'date' => '2016-03-14 11:38:07'
          ),
        )
      )
    )

答案 2 :(得分:0)

为您的代码添加了一些评论,希望这会有所帮助

Sub dateadd() ' Same name as a defined function, this will be a cause of Problems

Dim destSheet As Worksheet: Set sourceSheet = ThisWorkbook.Worksheets("Sheet1") 'the variable sourceSheet is neither initialised nor used
Dim Ldate As Date

Ldate = dateadd("d", 2, "6/14/2016") 'the vba function you want to call is DateAdd, but here vba will call the sub dateadd because it has the same name. Will throw an Wrong Argument Count error.

destSheet.Range("A1").Value = Ldate

End Sub