将活动工作表另存为新工作表的问题

时间:2013-12-17 11:07:31

标签: vba excel-vba excel

我正在尝试将第一张excel文件保存到目录C中:使用以下代码:

Sub SaveSheet()
    ActiveSheet.Copy
    With ActiveSheet.UsedRange
        .Copy
        .PasteSpecial xlValues
        .PasteSpecial xlFormats
    End With
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs "C:/" & Format(Range("E19"), "mmm-d-yyyy")
End Sub

但我在这里有两个问题:
首先我不知道在哪里设置新文件名?
2-我遇到以下警告信息:

enter image description here

虽然我已经启用了使用宏,如:

enter image description here

您能告诉我如何解决这些问题吗?

Thansk

更新: enter image description here

1 个答案:

答案 0 :(得分:4)

.SaveAs的语法是

expression.SaveAs(FileName, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local)

有关详细信息,请参阅Excel帮助。保存工作簿时,应指定的最少两个参数为FileNameFileFormat

如果要将文件另存为宏免费文件,则必须专门指定文件格式。

例如

ActiveWorkbook.SaveAs Filename:="C:\" & Format(Range("E19"), "mmm-d-yyyy") & ".xlsx", _
                      FileFormat:=xlOpenXMLWorkbook

或更简化的方法

Sub SaveSheet()
    Dim FName As String

    ActiveSheet.Copy
    With ActiveSheet.UsedRange
        .Copy
        .PasteSpecial xlValues
        .PasteSpecial xlFormats
    End With
    Application.CutCopyMode = False

    FName = "C:\" & Format(Range("E19"), "mmm-d-yyyy") & ".xlsx"

    ActiveWorkbook.SaveAs Filename:=FName, _
                          FileFormat:=xlOpenXMLWorkbook
End Sub

同样,如果要使用宏保存文件,上面的代码将变为

Sub SaveSheet()
    Dim FName As String

    ActiveSheet.Copy
    With ActiveSheet.UsedRange
        .Copy
        .PasteSpecial xlValues
        .PasteSpecial xlFormats
    End With
    Application.CutCopyMode = False

    FName = "C:\" & Format(Range("E19"), "mmm-d-yyyy") & ".xlsm"

    ActiveWorkbook.SaveAs Filename:=FName, _
                          FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub

以下是一些通用文件格式

50 = xlExcel12 (Excel Binary Workbook in 2007-2013 with or without macro's, xlsb)
51 = xlOpenXMLWorkbook (without macro's in 2007-2013, xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2013, xlsm)
56 = xlExcel8 (97-2003 format in Excel 2007-2013, xls)