vba:在xlsm fileformat中保存而不更改活动工作簿

时间:2014-08-08 23:52:47

标签: excel-vba save-as xlsm vba excel

我有以下代码来制作活动工作簿的副本,并为每个副本提供不同的名称。它运行良好,但我真的需要运行代码以保持活动的原始工作表。

如果我使用SaveCopyAs函数,则复制的文件没有正确的文件格式(.xlsm),并且您无法将文件格式指定为saveAs函数中的参数。

http://msdn.microsoft.com/en-us/library/bb178003%28v=office.12%29.aspx

http://msdn.microsoft.com/en-us/library/office/ff841185%28v=office.15%29.aspx

    Sub makeCopies()
        Dim name As Range, team As Range
        Dim uName As String, fName As String, fFormat As String
        Dim location as string, nName as string

        location ="c:\test\"
        nName = "Test - Team "
        Set team = Names("Team").RefersToRange

        For Each name In team
            uName = nName & name.Value
            fName = location & uName
            fFormat = ThisWorkbook.FileFormat
            ActiveWorkbook.SaveAs FileName:=fName, FileFormat:=fFormat
        Next name
   End sub

我能想到的最好的方法是首先使用saveCopyAs制作副本,然后访问每个文件,使用saveAs以正确的文件格式保存,然后关闭它,但这意味着双重工作,我真的很讨厌那样做。有更聪明的方法吗?

1 个答案:

答案 0 :(得分:0)

这是我的作品。 SaveCopyAs以完全相同的格式保存工作簿。

Sub makeCopies()
    Dim name As Range, team As Range
    Dim uName As String, fName As String, tempname As String
    Dim location As String, nName As String

    location = "C:\Test\"
    nName = "Test - Team "
    Set team = ThisWorkbook.Names("Team").RefersToRange

    For Each name In team
        uName = nName & name.Value
        fName = location & uName & "." & _
            Split(ThisWorkbook.FullName, ".") _
            (UBound(Split(ThisWorkbook.FullName, ".")))
        ThisWorkbook.SaveCopyAs fName
    Next name
End Sub

这是你正在尝试的吗?试过并测试过。