vba - 试图获得两个日期,上个月的 24 日和本月的 23 日

标签 vba excel date

我试图将两个日期作为变体,以便我可以将它们换入和换出每个月运行的 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

最佳答案

以下是来自操纵日期和格式掩码的一些字符串日期。

fromDateFinal = format(date-day(date), "yyyy-mm-24")  '2018-02-24
toDateFinal = format(date, "yyyy-mm-23")              '2018-03-23

关于vba - 试图获得两个日期,上个月的 24 日和本月的 23 日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49049962/

相关文章:

python - 通过excel使用python从bloomberg api获取数据

java - 使用Java进行日期格式转换

java - 如何使用java在mysql中插入时间

excel - 尝试使用 VBA 在 Chrome 中单击按钮

ms-access - 如何在 MS Access 数据库模块中使用 sftp?

vba - Excel VBA - 突出显示正数和负数对

excel - 如何修复 VBA 中的 "Execution Error 429 : ActiveX can' t 创建对象”错误

jsp - 从 scriplet 生成的日期设置为文本字段中的默认值

sql - MS Access VBA 选择查询

excel - 使用replace和find函数同时查找多个 "dot"值