VBA错误地将斜杠添加到格式化日期

时间:2017-08-14 20:41:37

标签: vba filepath

每周五使用格式创建一个文件:“Report 08 11 2017.xlsx”

Dim iWeekday As Integer, LastFridayDate As Date
iWeekday = Weekday(Now(), vbFriday)

LastFridayDate = Format(Now - (iWeekday - 1), "mm dd yyyy")

LastFridayDate的格式与它在文件路径中的显示方式相同,如'08 11 2017'。但是,当我尝试通过以下方式打开工作簿时:

Dim lw_report As Workbook

Set lw_report= Workbooks.Open("Report " & LastFridayDate & ".xlsx")

我收到错误:“无法找到报告08 \ 22 \ 2017.xlsx”

如何创建文件路径以保留空格?

1 个答案:

答案 0 :(得分:3)

LastFridayDate被定义为日期,因此当您尝试打开工作簿时,日期将与标准日期格式连接。

要简单地将dim LastFridayDate修复为字符串

Dim iWeekday As Integer, LastFridayDate As String
iWeekday = Weekday(Now(), vbFriday)

LastFridayDate = Format(Now - (iWeekday - 1), "mm dd yyyy")
Dim lw_report As Workbook

Set lw_report = Workbooks.Open("Report " & LastFridayDate & ".xlsx")