简体   繁体   English

无法关闭打开的excel工作簿

[英]unable to close opened excel workbook

I am building a excel file with commandbuttons, userforms etc. that contain links to other workbooks. 我正在使用命令按钮,用户表单等构建一个excel文件,其中包含指向其他工作簿的链接。

For the commandbuttons on my sheet I use the same code as for the commandbuttons in my userforms: 对于我工作表上的命令按钮,我使用的代码与我的userforms中的命令按钮相同:

workbooks.open "path"

with the userform commandbuttons ater this the following is added 使用userform命令按钮,然后添加以下内容

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 我必须首先激活我的工作簿,然后激活打开的工作簿,然后我可以关闭它

I have tried putting "unload me" befor and after the "workbooks.open" but this doesn't change anything. 我试过把“卸载我”放在“workbooks.open”之后,但这并没有改变任何东西。

I also tried the followin, also didn't work: 我也尝试了以下,也没有工作:

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

anyone any ideas? 任何想法?

Example of how it is now: Someone needs to make a price calculation. 现在的示例:有人需要进行价格计算。 they open the prices userform in my file. 他们在我的文件中打开价格userform。 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. 我不明白为什么他们不能第一次点击它,但他们可以在我和开放工作簿之间切换后点击它。

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. 如果您在显示之前没有创建自己的表单实例,VBA将在您卸载后使用它时执行各种松鼠事务。

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. 此外,从窗体中调用Unload Me后,请勿尝试运行任何其他代码。 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. 当然,表单的调用代码不在问题中,但也可以在那里解决。

Unload only affects a UserForm . Unload仅影响UserForm

Try adding a reference to the open workbook and then closing it like so: 尝试添加对打开的工作簿的引用,然后关闭它,如下所示:

dim wb as Workbook
set wb = Workbooks.Open "pathname"
wb.activate
' do whatever with it while it's open
wb.Close

If whatever you are doing is not automated, you will need a button on your form that will set the reference to the workbook and close it once the user has completed editing. 如果您正在执行的操作不是自动化的,则表单上将需要一个按钮,用于设置对工作簿的引用,并在用户完成编辑后将其关闭。

If you define the workbooks that you open as a variable - you will be able to close them easily. 如果您将作为变量打开的工作簿定义 - 您将能够轻松地关闭它们。 Keep in mind this does not save the workbook. 请记住,这不会保存工作簿。

Dim DataBook As Workbook
Set DataBook = Workbooks.open ("pathname")
' Do something useful with the file

DataBook.Close

It is not completely clear what you are trying to ask here, but I'll give it my best in providing you a full answer. 你在这里要问的问题并不完全清楚,但我会尽力为你提供一个完整的答案。 If you are having no success with the Unload Me statement when it comes to closing a user form , try specifying the full form name in VBA. 如果在关闭用户表单时没有成功使用Unload Me语句,请尝试在VBA中指定完整的表单名称。 That is: 那是:

Unload UserFormName

If you are trying to close the workbook you have opened (via the user form), you can use the Workbooks.Close method: 如果您尝试关闭已打开的工作簿 (通过用户表单),则可以使用Workbooks.Close方法:

Dim wb as Workbook
Set wb = Workbooks.Open(Filename:="C:\example.xls") 
wb.Activate
'Close workbook
wb.Close
'Unload form
Unload ExampleForm

More on the use of the Unload statement can be found here:- https://msdn.microsoft.com/en-us/library/aa445829(v=vs.60).aspx 有关使用Unload语句的更多信息,请访问: - https://msdn.microsoft.com/en-us/library/aa445829(v=vs.60).aspx

And more on the use of the Workbooks.Close statement can be found here:- https://msdn.microsoft.com/en-us/library/office/ff838613.aspx 更多关于Workbooks.Close语句的使用可以在这里找到: - https://msdn.microsoft.com/en-us/library/office/ff838613.aspx

I'm not sure what's causing the behavior you describe (inability to click the "Close" X button in the active Excel window). 我不确定是什么原因导致您描述的行为(无法单击活动Excel窗口中的“关闭”X按钮)。 However your current workflow is: 但是您当前的工作流程是:

  1. User interacts with form to review/access a file 用户与表单交互以查看/访问文件
  2. User clicks a button on the form which unloads the form 用户单击表单上的一个按钮来卸载表单
  3. User is then expected to manually close out of the file opened in (1) 然后,用户需要手动关闭(1)中打开的文件

A better solution would be to remove the third step, and close the workbook from one of the form's event-handlers ( UserForm_QueryClose or UserForm_Terminate ). 更好的解决方案是删除第三步,并从表单的事件处理程序之一( UserForm_QueryCloseUserForm_Terminate )关闭工作簿。 Alternatively you could add this to a CommandButton 's Click event, but that's not very different than requiring the user to manually close the file. 或者,您可以将其添加到CommandButtonClick事件中,但这与要求用户手动关闭文件没有什么不同。

This could be as simple as: 这可能很简单:

Private Sub UserForm_Terminate()
' should be called when form Unloads
On Error Resume Next
Workbooks.Close "filename.xlsx"
End Sub

In this manner, the file will be closed any time the user closes or otherwise unloads the userform. 以这种方式,文件将在用户关闭或以其他方式卸载用户表单时关闭。

OK I think I found a way around it. 好吧,我想我找到了解决办法。

Let's say I have a workbook1 which has a form called ControlPage with command button that opens up workbook2. 假设我有一个工作簿1,它有一个名为ControlPage的表单,带有命令按钮,可以打开工作簿2。

Private Sub CommandButton1_Click() Private Sub CommandButton1_Click()

Dim workbook2 As Workbook
Dim workbook1name As String
workbook1name = ThisWorkbook.name

Set workbook2 = Workbooks.Open("workbook2.xlsx")
ControlPage.Hide
Workbooks(workbook1name).Activate
Workbooks("workbook2.xlsx").Activate

End Sub

So basically because 'activate' bit doesn't seem to work properly when I try to activate workbook 2 straight away, I just activate the first, and then the second one ane after another. 因此,当我尝试直接激活工作簿2时,'激活'位似乎不能正常工作,我只是激活第一个,然后第二个激活一个接一个。 Twisted but did the trick for me. 扭曲,但为我做了伎俩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM