使用时间戳保存时出现类型不匹配错误

Type mismatch error when saving with timestamp

我的下面代码停在“另存为”行并出现类型不匹配错误,知道为什么吗?我真的需要 YYYY-MM-DD 格式的日期。我有有效的代码,但它会将一位数的月份和日期显示为一位数,而不是 04。

Sub save2()
'APAC
Application.ScreenUpdating = False
Dim wb As Workbook
Dim xStrDate As String
Set wb = Workbooks.Add
xStrDate = VBA.Format(Now(), "yyyy-mm-dd")
ThisWorkbook.Sheets("APAC_Coming_Due").Copy Before:=wb.Sheets(1)
ThisWorkbook.Sheets("APAC_Overdue").Copy Before:=wb.Sheets(1)
ThisWorkbook.Sheets("APACCover").Copy Before:=wb.Sheets(1)
wb.Sheets(1).Name = "APAC - IMPORTANT INFORMATION"
wb.Sheets(2).Name = "Overdue"
wb.Sheets(3).Name = "Due Date Approaching"
Application.DisplayAlerts = False
wb.Sheets("Sheet1").Delete


wb.SaveAs"\xxxxx.xx.xxxxx.com\xxxxxx\xxxx\xxxxxxx\xxxxxx\xxxxxx\xxxxx\xxxx_
\xxxxx\xxxx\xxxx\APAC OverdueOutstanding & " - " & xStrDate.xlsx"
ActiveWorkbook.Close
End Sub

您需要在字符串外部而非内部使用 & 正确连接字符串和变量!

如果有换行符,则需要以 " & _ 结束字符串并在下一行开始一个新字符串:

wb.SaveAs "\xxxxx.xx.xxxxx.com\xxxxxx\xxxx\xxxxxxx\xxxxxx\xxxxxx\xxxxx\xxxx" & _ 
          "\xxxxx\xxxx\xxxx\APAC OverdueOutstanding - " & xStrDate & ".xlsx"
wb.Close

或者将所有内容都放在一行中:

wb.SaveAs "\xxxxx.xx.xxxxx.com\xxxxxx\xxxx\xxxxxxx\xxxxxx\xxxxxx\xxxxx\xxxx\xxxxx\xxxx\xxxx\APAC OverdueOutstanding - " & xStrDate & ".xlsx"
wb.Close