【问题标题】:Macro that saves the file as macro-free closes the original file将文件另存为无宏的宏会关闭原始文件
【发布时间】:2014-04-20 08:32:29
【问题描述】:

这是数据操作宏最后一部分的 sn-p:

    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 行后停止 - 后续行永远不会执行。)

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    删除这一行

    Workbooks.Open Filename:=newFileFullName
    

    在执行ActiveWorkbook.SaveAs 之后,您的活动工作簿已经引用了Aspects List.xlsx

    SaveAs之前:​​

    SaveAs之后:

    顺便说一句,在我看来

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

    可以简化为

    newFilePath = ThisWorkbook.Path & "\"
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      相关资源
      最近更新 更多