如何将选定的工作表保存为xlsx

时间:2014-06-26 18:51:59

标签: excel vba excel-vba

我正在运行Windows 7 x64和Office 2010 x64。我有一个VBA代码问题,用于将文件保存为.xlsx。如果我将它保存为.xls,一切都很好,但我需要将其保存为.xlsx。这是我的代码:

Sub Save_Alignment()

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False

End With


Dim Fpath    As String
Dim Fname   As String
Dim FileExtStr As String
Dim FileFormatNum As Long

FileExtStr = ".xls"
FileFormatNum = 56

Fpath = "C:\Users\Me\Documents\"
Fname = "Document " & Format(Date, "yyyy-mm-dd") & FileExtStr


Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets 'SetVersions
    If ws.Name = "CFGBranchAlignment" Then
        Dim wb As Workbook
        Set wb = ws.Application.Workbooks.Add
        ws.Copy Before:=wb.Sheets(1)
        wb.SaveAs Fpath & Fname, FileFormat:=FileFormatNum
        Set wb = Nothing
    End If

Next ws
Workbooks("Document MASTER.xlsm").Activate
Workbooks(Fname).Close SaveChanges:=False

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True

End With

End Sub

这样运行正常,直到我将FileExtStr更改为.xlsx并将FileFormatNum更改为51。 然后代码在wb.SaveAs Fpath ... line断开。

我在Fpath之后也尝试过以下一行:

wb.SaveAs path & Fname, Excel.XlFileFormat.xlOpenXMLWorkbook

我正在从启用宏的工作簿中复制它(如果有帮助的话)。

在这里获得代码的基础知识:

Use VBA Macro to Save each Excel Worksheet as Separate Workbook

但是除非我将文件保存在Old excel版本中,否则我无法让它运行错误。 在我的智慧结束,所以任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

要将文件另存为.xlsx,请更改文件扩展名和FileformatNum

FileExtStr = ".xlsx"
FileFormatNum = 51

答案 1 :(得分:0)

嗨,迈克尝试以下代码......它工作正常 还要确保您的系统中安装了2007或2010版本或至少“文件转换” 如果您使用的是2003格式,如果您没有2007或2010版本,则代码将会中断。

Sub Save_Alignment()

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False

End With


Dim Fpath    As String
Dim Fname   As String
Dim FileExtStr As String
Dim FileFormatNum As Long

FileExtStr = ".xlsx"
FileFormatNum = 51

Fpath = "C:\"
Fname = "Document " & Format(Date, "yyyy-mm-dd") & FileExtStr


Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets 'SetVersions
    If ws.Name = "CFGBranchAlignment" Then
        Dim wb As Workbook
        Set wb = ws.Application.Workbooks.Add
        ws.Copy Before:=wb.Sheets(1)
        wb.SaveAs Fpath & Fname, FileFormat:=FileFormatNum
        Set wb = Nothing
    End If

Next ws
Workbooks("Document MASTER.xlsm").Activate
Workbooks(Fname).Close SaveChanges:=False

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True

End With

End Sub

答案 2 :(得分:0)

试试这个。在 For Each 子句中进行了一些修改:

Sub test()
    Dim Fname As String, Fpath As String, FileExtStr As String

    FileExtStr = ".xls"
    Fpath = CreateObject("WScript.Shell").specialfolders("MyDocuments") & "\"
    Fname = "Document " & Format(Date, "yyyy-mm-dd") & FileExtStr

    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name = "CFGBranchAlignment" Then
            Dim wb As Workbook
            ws.Copy
            Set wb = ActiveWorkbook
            wb.SaveAs Fpath & Fname, xlOpenXMLWorkbook
            Set wb = Nothing
        End If
    Next ws
End Sub

HTH。顺便说一句,坚持直接使用你的对象。
Check this out了解如何避免选择和激活[对象]的方法。