2013-02-20 83 views
1

在VBA脚本中,我需要修改日期&时间。即增加或减少一天,根据条件将时间设定为上午8点或晚上8点。Excel修改日期和时间

我这里有相关的代码段 -

变量声明 -

' 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) 

问: 我所做的是递减的日期和设置时间上午八时至,当条件满足。我收到一个语法错误。请帮忙。

回答

3

您需要TimeSerialDateSerialTIME更换DATE

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) 
+0

太谢谢你了。 – Selvam 2013-02-20 10:16:59