How to do a “Save As” in vba code, saving my current Excel workbook with datestamp?

前端 未结 7 1458
旧时难觅i
旧时难觅i 2020-12-03 07:35

I have an Excel Workbook that on form button click I want to save a copy of the workbook with the filename being the current date.

I keep trying the the following

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 08:32

    It could be that your default format doesn't match the file extension. You should specify the file format along with the filename, making sure the format matches the extension:

    With someWorkbook
    .SaveAs "C:\someDirector\Awesome.xlsm", fileformat:=xlOpenXMLWorkbookMacroEnabled
    End With
    

    OTOH, I don't see an extension on your .SaveAs filename. Maybe you need to supply one when doing this programmatically. That makes sense--not having to supply an extension from the GUI interface is convenient, but we programmers are expected to write unambiguous code. I suggest adding the extension and the matching format. See this msdn page for a list of file formats. To be honest, I don't recognize a lot o the descripions.

    xlExcel8 = 56 is the .xls format

    xlExcel12 = 50 is the .xlsb format

    xlOpenXMLWorkbook = 51 is the .xlsx format

    xlOpenXMLWorkbookMacroEnabled = 52 is the .xlsm format

    xlWorkbookDefault is also listed with a value of 51, which puzzles me since I thought the default format could be changed.

提交回复
热议问题