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

前端 未结 7 1416
旧时难觅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:13
    Dim NuevoLibro As Workbook
    Dim NombreLibro As String
        NombreLibro = "LibroPrueba"
    '---Creamos nuevo libro y lo guardamos
        Set NuevoLibro = Workbooks.Add
            With NuevoLibro
                .SaveAs Filename:=NuevaRuta & NombreLibro, FileFormat:=52
            End With
                                                        '*****************************
                                                            'valores para FileFormat
                                                            '.xlsx = 51 '(52 for Mac)
                                                            '.xlsm = 52 '(53 for Mac)
                                                            '.xlsb = 50 '(51 for Mac)
                                                            '.xls = 56 '(57 for Mac)
                                                        '*****************************
    
    0 讨论(0)
  • 2020-12-03 08:20

    Easiest way to use this function is to start by 'Recording a Macro'. Once you start recording, save the file to the location you want, with the name you want, and then of course set the file type, most likely 'Excel Macro Enabled Workbook' ~ 'XLSM'

    Stop recording and you can start inspecting your code.

    I wrote the code below which allows you to save a workbook using the path where the file was originally located, naming it as "Event [date in cell "A1"]"

    Option Explicit
    
    Sub SaveFile()
    
    Dim fdate As Date
    Dim fname As String
    Dim path As String
    
    fdate = Range("A1").Value
    path = Application.ActiveWorkbook.path
    
    If fdate > 0 Then
        fname = "Event " & fdate
        Application.ActiveWorkbook.SaveAs Filename:=path & "\" & fname, _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    Else
        MsgBox "Chose a date for the event", vbOKOnly
    End If
    
    End Sub
    

    Copy the code into a new module and then write a date in cell "A1" e.g. 01-01-2016 -> assign the sub to a button and run. [Note] you need to make a save file before this script will work, because a new workbook is saved to the default autosave location!

    0 讨论(0)
  • 2020-12-03 08:27

    I know this is an old post but I was looking up something similar... I think your issue was that when you use Now(), the output will be "6/20/2014"... This an issue for a file name as it has "/" in it. As you may know, you cannot use certain symbols in a file name.

    Cheers

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 08:37

    I successfully use the following method in one file,

    But come up with exactly the same error again... Only the last line come up with error

    Newpath = Mid(ThisWorkbook.FullName, 1, _
     Len(ThisWorkbook.FullName) - Len(ThisWorkbook.Name)) & "\" & "ABC - " & Format(Date, "dd-mm-yyyy") & ".xlsm"
    ThisWorkbook.SaveAs (Newpath)
    
    0 讨论(0)
  • 2020-12-03 08:40

    Most likely the path you are trying to access does not exist. It seems you are trying to save to a relative location and you do not have an file extension in that string. If you need to use relative paths you can parse the path from ActiveWorkbook.FullName

    EDIT: Better syntax would also be

    ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlWorkbookNormal
    
    0 讨论(0)
提交回复
热议问题