如何修改Excel 2007的代码以使用SaveAs
我有一个使用macros保存文件的Excel模板,以便用户维护标准化的文件名格式。 我使用Excel 2003创build了代码。代码如下:
Sub SaveBook() Dim sFile As String sFile = "ConsolidatedDemand" & "_" & Format(Now(), "yyyy.mm.dd") & ".xls" ActiveWorkbook.SaveAs Filename:= "\\file location\" & sFile End Sub
我有一个使用Excel 2007的用户。当他们尝试运行macros时,他们收到一个错误:“以下function不能保存在无macros工作簿:VB项目。要保存具有这些function的文件,请单击否,然后在文件types列表中select启用macros的文件types要继续保存为无macros的工作簿,请单击“是”
我尝试在第二行代码中将文件扩展名修改为“.xlsm”,但是却得到相同的错误信息。 有关如何修改此代码的其他任何想法,以便Excel 2007用户可以使用该代码?
(摘自: http : //blogs.office.com/b/microsoft-excel/archive/2009/07/07/use-the-vba-saveas-method-in-excel-2007.aspx )
在Excel 2007中,SaveAs方法要求您提供FileFormat参数和正确的文件扩展名。
例如,在Excel 2007中,如果ActiveWorkbook不是.xlsm文件,则这行代码将失败:
ActiveWorkbook.SaveAs "C:\ron.xlsm"
但是这个代码将一直工作:
ActiveWorkbook.SaveAs "C:\ron.xlsm", fileformat:=52
这些是Excel 2007中的主要文件格式:
- 51 = xlOpenXMLWorkbook(2007年没有macros,.xlsx)
- 52 = xlOpenXMLWorkbookMacroEnabled(有或没有在2007年的macros,.xlsm)
- 50 = xlExcel12(2007年有或没有macros的.xlsb的Excel二进制工作簿)
- 56 = xlExcel8(Excel 2007中的97-2003格式,.xls)
只要您不知道,您可以检查应用程序在macros中的版本,这样您可以确定是否应该保存为“xls”或“xlsm”
If Application.Version = "13.0" Then MsgBox "You are using Excel 2010." ElseIf Application.Version = "12.0" Then MsgBox "You are using Excel 2007." ElseIf Application.Version = "11.0" Then MsgBox "You are using Excel 2003." ElseIf Application.Version = "10.0" Then MsgBox "You are using Excel 2002." ElseIf Application.Version = "9.0" Then MsgBox "You are using Excel 2000." ElseIf Application.Version = "8.0" Then MsgBox "You are using Excel 97." ElseIf Application.Version = "7.0" Then MsgBox "You are using Excel 95." Else MsgBox "You are using an unknown version of Excel: " & Application.Version End If
希望这可以帮助。