有没有办法在 VBA 中覆盖 Now() 以进行测试?像这样的东西 - What's a good way to overwrite DateTime.Now during testing? ,但对于 VBA。
因此,我需要 Now()
返回预定义值而不是当前 PC 时间。
我做什么不是 想要是这样的:
Option Explicit
Public Function NowDefined() As Date
'NowDefined = Now()
NowDefined = Format("2016-01-15 15:01:01", "yyyy-MM-dd hh:mm:ss")
End Function
并简单地更改整个代码
Now()
至NowDefined()
.
最佳答案
而不是创建 String
变量,然后让 Excel 尝试将其转换为 Date
,您最好生成实际的开始日期:
Function Now() As Date
Now = DateSerial(2016, 1, 15) + TimeSerial(15, 1, 2)
End Function
关于excel - 覆盖 VBA 的 Now() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543847/