Home > database >  Type mismatch error when saving with timestamp
Type mismatch error when saving with timestamp

Time:10-16

My code below stops at the "SaveAs" line with a type mismatch error, any idea why? I really need to have the date in a YYYY-MM-DD format. I had code that worked but it would show single digit months and days as a single digit, instead of 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

CodePudding user response:

You need to concatenate your strings and variables correctly using & outside a string not inside!

And if you have line breaks you need to end the string with " & _ and start a new string in the next line:

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

or have everything in one line:

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