VBA代码另存为.XLSM

时间:2018-02-11 16:02:42

标签: excel-vba vba excel

需要协助添加命令以另存为.xlsm: -

Private Sub cmdSaveForm1_Click()
    Dim strFolder As String
    Dim i As Long

    'Find the position of the period in the file name
    i = InStr(ActiveWorkbook.Name, ".")

    'Create a default file name by concatenating the file name without the extention _
        plus the current date and time, and plus the xlsm extention
    Filename = Left(ActiveWorkbook.Name, i - 1) & "_" & Format(Now, "yyyy-mm-dd_hh mm") & ".xlsm"

    'Open Save As dialog to a default folder with default file name
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = "P:\EU Funds Management - Treasury\TRS3_Abstract of Payments\TRS3_Authorisation_L1\" & Filename
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
        .Execute
    End With
End Sub

2 个答案:

答案 0 :(得分:2)

要将工作簿另存为.xlsm,您需要以下文件格式

  

Excel 2007-2010支持宏的工作簿(.xlsm) - 52 - xlOpenXMLWorkbookMacroEnabled

要将文件保存为所选格式,您需要在保存时指定适当的格式。这可以通过在保存操作中添加FileFormat:=来完成。

ThisWorkbook.SaveAs Filename:=Path & Filename, FileFormat:=xlOpenXMLWorkbookMacroEnabled

在保存操作和FileFormat添加到您的代码之后。

Private Sub cmdSaveForm1_Click()
    Dim strFolder As String
    Dim i As Long

    'Find the position of the period in the file name
    i = InStr(ActiveWorkbook.Name, ".")

    'Create a default file name by concatenating the file name without the extention _
        plus the current date and time, and plus the xlsm extention
    Filename = Left(ActiveWorkbook.Name, i - 1) & "_" & Format(Now, "yyyy-mm-dd_hh mm") & ".xlsm"

    'Open Save As dialog to a default folder with default file name
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = "P:\EU Funds Management - Treasury\TRS3_Abstract of Payments\TRS3_Authorisation_L1\" & Filename
        .InitialView = msoFileDialogViewDetails

        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub

        'get selected folder path from FileDialog, but remove filename from FileDialog
        folderPath = Left(strFolder, InStrRev(strFolder, "\"))

        'Save this workbook in chosen file path & appropriate filename
        'File format .xlsm
        ThisWorkbook.SaveAs Filename:=folderPath & Filename, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End With
End Sub

答案 1 :(得分:0)

解决方案是:
.FilterIndex = 2
1 = xlsx,2 = xlsm

Private Sub cmdSaveForm1_Click()

    Dim strFolder As String
    Dim i As Long

    'Find the position of the period in the file name
    i = InStr(ActiveWorkbook.Name, ".")

    'Create a default file name by concatenating the file name without the extention _
        plus the current date and time, and plus the xlsm extention
    Filename = Left(ActiveWorkbook.Name, i - 1) & "_" & Format(Now, "yyyy-mm-dd_hh mm") & ".xlsm"

    'Open Save As dialog to a default folder with default file name
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .FilterIndex = 2  '2 = xlsm
        .InitialFileName = "P:\EU Funds Management - Treasury\TRS3_Abstract of Payments\TRS3_Authorisation_L1\" & Filename
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
        .Execute
    End With
End Sub