1

在 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 点,条件满足时。我收到语法错误。请帮忙。

4

1 回答 1

3

您需要DATEDateSerialTIME替换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)
于 2013-02-20T09:31:42.157 回答