【问题标题】:Excel modify date & timeExcel修改日期和时间
【发布时间】:2013-02-05 04:44:18
【问题描述】:

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

【问题讨论】:

    标签: vba date


    【解决方案1】:

    您需要将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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 2021-07-14
      相关资源
      最近更新 更多