将文件保存为无宏的宏将关闭原始文件

时间:2014-04-20 08:32:30

标签: excel vba excel-vba

以下是数据操作宏的最后一部分的片段:

    Cells.EntireColumn.AutoFit

    Application.ScreenUpdating = True

    Dim fullfilenamelength As Integer, filenamelength As Integer
    fullfilenamelength = Len(ThisWorkbook.FullName)
    filenamelength = Len(udfWBFilename("ThisOne"))

    Dim newFilePath As String, newFileFullName As String
    newFilePath = Left(ThisWorkbook.FullName, fullfilenamelength - filenamelength)
    newFileFullName = newFilePath & "Aspects List.xlsx"

    ActiveWorkbook.SaveAs Filename:=newFileFullName, FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False

    Workbooks.Open Filename:=newFileFullName
    Windows("Aspects List.xlsx").Activate

    Beep
    Application.DisplayAlerts = True
    Application.StatusBar = False
End Sub

最后,它将文件保存为无宏工作簿,然后打开新文件。

为什么这样做会关闭旧文件? (换句话说,在运行行Windows("Aspects List.xlsx").Activate后停止宏执行 - 后续行永远不会被执行。)

1 个答案:

答案 0 :(得分:1)

只需删除此行

即可
Workbooks.Open Filename:=newFileFullName

执行ActiveWorkbook.SaveAs后,您的有效工作簿已经引用Aspects List.xlsx

SaveAs之前:

enter image description here

SaveAs后:

enter image description here

顺便说一下,在我看来

newFilePath = Left(ThisWorkbook.FullName, fullfilenamelength - filenamelength)

可以简化为

newFilePath = ThisWorkbook.Path & "\"

也可能有趣:How to avoid using Select/Active statements