VBA - 关闭窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7202201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA - Close window
提问by David
I have a macro that creates 17 workbooks in a loop. When it's done working on a workbook it closes it and goes to the next with:
我有一个可以循环创建 17 个工作簿的宏。完成工作簿上的工作后,它会关闭它并转到下一个:
ActiveWorkbook.Save
ActiveWindow.Close
This has always worked fine until recently. Suddenly on loop nr. 7 it say (the 6 loops before still working fine):
直到最近,这一直运行良好。突然循环nr。7 它说(仍然工作正常之前的 6 个循环):
Run-time '-2147417848 (80010108)':
Method 'Close' of objekt 'Window' failed
运行时'-2147417848(80010108)':
对象“窗口”的方法“关闭”失败
I can delete "ActiveWindow.Close" and close all the workbooks manually to make the macro work. But still...
我可以删除“ActiveWindow.Close”并手动关闭所有工作簿以使宏工作。但是还是...
Any knows why it does this?
有谁知道为什么这样做?
回答by Fionnuala
As per comment.
根据评论。
A wait period may solve this problem.
等待期可以解决这个问题。
回答by aevanko
I'm not sure why you are using ActiveWindow.Close, perhaps that has something to do with it. The proper way to close a book (as far as I know) is:
我不确定您为什么使用 ActiveWindow.Close,也许这与它有关。关闭一本书的正确方法(据我所知)是:
Workbooks(excelFile).Close SaveChanges:=True
Sometimes some wacky things happen when refering to the active object, so it's best to be explicit.
有时在引用活动对象时会发生一些古怪的事情,所以最好是明确的。
回答by Jonathan
I use the number of the window... i.e. I know this will be Window #1, so I refer to it that way. Example:
我使用窗口的编号...即我知道这将是窗口 #1,所以我是这样引用的。例子:
Windows(1).Close SaveChanges:=False