Vba Excel修改日期&;时间
在VBA脚本中,我需要修改日期和时间。i、 e.增加或减少一天,根据情况将时间设置为上午8点或晚上8点 我这里有相关的代码段- 声明的变量-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
' 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)