将文件另存为无宏的宏会关闭原始文件
Posted
技术标签:
【中文标题】将文件另存为无宏的宏会关闭原始文件【英文标题】: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
行后停止 - 后续行永远不会执行。)
【问题讨论】:
【参考方案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
【讨论】:
以上是关于将文件另存为无宏的宏会关闭原始文件的主要内容,如果未能解决你的问题,请参考以下文章