Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Excel修改日期&;时间_Vba_Date - Fatal编程技术网

Vba Excel修改日期&;时间

Vba Excel修改日期&;时间,vba,date,Vba,Date,在VBA脚本中,我需要修改日期和时间。i、 e.增加或减少一天,根据情况将时间设置为上午8点或晚上8点 我这里有相关的代码段- 声明的变量- ' To reference the cell having the source date Dim d As String ' To reference the cell where the modified date is written Dim destCell As String ' Some cell that contains 1 or

在VBA脚本中,我需要修改日期和时间。i、 e.增加或减少一天,根据情况将时间设置为上午8点或晚上8点

我这里有相关的代码段-

声明的变量-

' To reference the cell having the source date 
Dim d As String 

' To reference the cell where the modified date is written
Dim destCell As String

' Some cell that contains 1 or 0
Dim somecell As String

'If condition and value assignment

If Range(somecell).Value = 1 Then
    Range(destCell).Value = DATE(YEAR(Range(d)),MONTH(Range(d)),DAY(Range(d)-1))+TIME(8,0,0)
问题:
我所做的是减少一天的时间,当条件满足时,将时间设置为上午8点。我得到一个语法错误。请帮助。

您需要将
日期
替换为
日期序列
,将
时间
替换为
时间序列

Dim datSource As Date
datSource = Range(d).Value
If Range(somecell).Value = 1 Then
    Range(destCell).Value = _
        DateSerial(Year(datSource), Month(datSource), Day(datSource) - 1) + _
        TimeSerial(8, 0, 0)