VBA创建Excel .xlsm vs .xlsx

时间:2016-04-07 15:39:50

标签: vba excel-vba excel

您好,感谢您阅读我的问题,我有一个隐藏模板的工作簿,其中大部分都用作Excel .xlsx电子表格,但其中一个需要插入一个模块才能工作。我觉得很简单,只需在创建工作簿的函数中添加一个参数。

它似乎无法正常工作,因为我收到错误"错误号1004此扩展名不能与所选的文件类型一起使用。在文件名文本框中更改文件扩展名,或选择其他文件类型blabla"

Public Function gWrkBook(template As String, Optional wbMacro As Boolean) As Workbook
    Dim wbNew As Workbook
    Dim wsTemplate As Worksheet, wsSummary As Worksheet


Set wsTemplate = ThisWorkbook.Worksheets(template) '===== Create new workbook and copy template
wsTemplate.Visible = True
'
Set wbNew = Workbooks.Add                          'Create New file
wsTemplate.Copy Before:=wbNew.Sheets(1)            'Copy template to new workbook
                                                   'Rename sheet
On Error GoTo ErrSheetName
    wbNew.Sheets(1).Name = "SUMMARY"

Set wsSummary = wbNew.Sheets("SUMMARY")


wsTemplate.Visible = False '===== Clean up
Call gRemoveUnwanted("sheets", wbNew) 'Mod7 '==== Get SaveAs filename and save file


If wbMacro = True Then
    vFileName = Application.GetSaveAsFilename(Filname, "Excel Macro-Enabled workbook(*.xlsm), *.xlsm", Title:="SaveAs Workbook Macro-Enabled")
Else
    vFileName = Application.GetSaveAsFilename(FileFilter:="Microsoft Excel Workbooks, *.xlsx", Title:="SaveAs Workbook")
End If

On Error GoTo ErrFileName
    wbNew.SaveAs Filename:=vFileName

Set gWrkBook = wbNew 'must assign it this way?? not sure why R2

Exit Function

ErrSheetName:
NewSheetName = InputBox("Worksheet exists, try a different name." & vbCrLf & "Enter Sheet Name.")
Resume

ErrFileName:
MsgBox "Error Number " & _
        Err.Number & vbCrLf & _
        Error(Err) & vbCrLf & _
        "Try Again!", _
        vbExclamation + vbOKOnly, _
        "ERROR!"
vFileName = Application.GetSaveAsFilename(FileFilter:="Microsoft Excel Workbooks, *.xls; *.xlsx", _
                                        Title:="SaveAs Workbook")
Resume

End Function

1 个答案:

答案 0 :(得分:2)

尝试:

wbNew.SaveAs(Filename:=vFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled)


有关详细信息,请阅读文档: