2017-08-14 85 views
2

的文件創建的每個週五的格式:「報告08 11 2017.xlsx」VBA錯誤地增加了斜線格式的日期

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」無法找到

我怎樣才能創建,這樣的文件路徑空間保存了嗎?

回答

3

LastFridayDate被定義爲一個日期,所以當你試圖打開日期的值與標準的日期格式的工作簿。

要解決這個問題簡單地dim LastFridayDate爲String

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") 
+0

謝謝,這個固定我的probblem。 –