【发布时间】:2018-08-09 12:53:34
【问题描述】:
我正在尝试获取两个日期作为变体,以便可以将它们换入和换出每个月运行的 SQL 查询(具有不同的时期)。我试图以"yyyy-mm-dd" 格式获取上个月的 24 日,以"yyyy-mm-dd" 格式获取本月的 23 日(程序运行时)。我有这段代码,但它给了我“错误数量的参数”作为错误,我不明白为什么。
Option Explicit
Sub mDateSet()
Dim fromDateFinal As String
Dim toDate As String
Dim toDateFinal As String
Dim fromDate: fromDate = Format(DateAdd("M", -1, Now), "yyyy-mmmm")
'Format gives 'wrong number of arguments or invalid property assignment error'
'trying to define two dates: the 24th of last month until the 23rd of this month)
'24th of last month
fromDateFinal = fromDate & "24"
Debug.Print fromDateFinal
'23th of this month
toDate = Format(Date, "yyyy-mm")
toDateFinal = toDate & "23"
Debug.Print toDateFinal
End Sub
【问题讨论】:
-
如果我运行这段代码,它会打印出
2018-Februar24和2018-0323。因此,您应该添加 2 个破折号以获得良好的日期格式,但它在此处运行时没有任何错误。无法重现它。您确定问题中的代码正是您尝试过的代码吗? -
问题是@Axel Richter 说
Format是在项目中定义的,这就是我收到错误的原因。否则代码会(有点)功能,这就是为什么我不明白为什么它会给我一个错误。