在VBA脚本中,我需要修改日期&时间。即增加或减少一天,根据条件设定时间为上午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点,条件满足时。我收到语法错误。请帮忙。
答案 0 :(得分:3)
您需要将DATE
替换为DateSerial
,将TIME
替换为TimeSerial
:
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)