无法关闭已打开的Excel工作簿 [英] unable to close opened excel workbook

查看:192
本文介绍了无法关闭已打开的Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个包含命令按钮,用户窗体等的excel文件,其中包含指向其他工作簿的链接.

I am building a excel file with commandbuttons, userforms etc. that contain links to other workbooks.

对于工作表上的命令按钮,我使用与用户表单中的命令按钮相同的代码:

For the commandbuttons on my sheet I use the same code as for the commandbuttons in my userforms:

workbooks.open "path"

使用userform命令按钮,然后添加了以下内容

with the userform commandbuttons ater this the following is added

unload me

当我通过用户窗体打开工作簿时,我以后将无法关闭它.我必须先激活我的工作簿,然后激活打开的工作簿,然后才能关闭它

When I open a workbook via a userform I am unable to close it afterwards. I must activate my workbook first, then activate the opened one and then can I close it

我尝试在"workbooks.open"之前和之后放置卸载我",但这没有任何改变.

I have tried putting "unload me" befor and after the "workbooks.open" but this doesn't change anything.

我也尝试了以下操作,但也没有用:

I also tried the followin, also didn't work:

unload me
dim wb as workbook
set wb = workbooks.open"pathname"
wb.activate

任何人有什么想法吗?

现在的示例: 有人需要进行价格计算.他们在我的文件中打开价格用户表单.他们单击按钮"calculationfile".计算文件打开.他们在那里进行计算,现在它们已经在计算文件中完成了.因此,他们想通过单击十字架将其关闭.但是他们不能点击十字架.然后他们在任务栏上切换到我的文件,然后切换回计算文件.现在他们可以单击十字了

Example of how it is now: Someone needs to make a price calculation. they open the prices userform in my file. they click on the button "calculationfile". The calculationfile opens. they make there calculation and now they are finished in the calculationfile. So they want to close it by clicking on the cross. But they can't click the cross. then they switch to my file on the taskbar and then switch back to the calculation file. now they are able of clicking the cross

我不明白为什么他们第一次不能单击它,但是他们在我的和开放式工作簿之间切换后才可以单击它.

I dont understand why they can't click it the first time but they can click it after switching between mine and the openend workbook.

推荐答案

我怀疑这是由于不正确的表单处理以及在卸载后重新创建自身的默认实例"所致.如果在显示表单之前未创建自己的表单实例,则在卸载后使用它时,VBA会做各种松散的事情.

I suspect this is due to improper form handling and the "default instance" recreating itself after you unload it. If you don't create your own instance of the form before you show it, VBA will do all kinds of squirrelly things when you use it after it's unloaded.

如果您需要做的只是打开工作簿并卸载表单,那么*在卸载表单之前不要增加工作簿的引用计数.另外,从窗体调用Unload Me后,请勿尝试运行任何其他代码.您发布的代码应该只是:

If all you need to do is open a workbook and unload the form, *don't increment the workbook's reference count before you unload the form. Also, don't attempt to run any other code after you call Unload Me from the form. The code you posted should simply be:

Workbooks.Open "pathname"
Unload Me

当然,表单的调用代码不是问题,但也可以在那里解决.

Of course the calling code for the form isn't in the question, but it can likely be solved there as well.

这篇关于无法关闭已打开的Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆