Excel 2013 – 当隐藏一个工作簿时closures多个工作簿时出现问题

我已经在Excel VBA中编写了一个程序,它使用用户窗体来input用户的数据。 具体而言,它是一个电话营销跟踪工具:用户在用户窗体上的文本框中填写呼叫的详细信息,然后单击相关button以指示是否是好的或不好的呼叫,然后可以继续下一个呼叫。

此数据存储在工作表上,我们的用户通常喜欢隐藏工作簿,并查看UserForm。 我已经开发了一些隐藏工作簿的方法。 如果只打开一个工作簿,则隐藏Excel应用程序。 如果有多个工作簿打开,我只是隐藏窗口。 这里是我使用的代码:

Private Sub HideUnhideButton_Click() 'User clicks Hide/Unhide button If Workbooks.Count > 1 Then Windows(ThisWorkbook.Name).Visible = Not Windows(ThisWorkbook.Name).Visible HideUnhideButton.Tag = Windows(ThisWorkbook.Name).Visible Else ThisWorkbook.Application.Visible = Not ThisWorkbook.Application.Visible HideUnhideButton.Tag = ThisWorkbook.Application.Visible End If ThisWorkbook.Activate End Sub 

这很好,但显然当用户隐藏工作簿,然后打开不同的Excel工作簿时会出现某些问题。 我已经解决了大部分这些问题,但有一件事情我看起来似乎没有解决: 如果电话营销工作簿处于隐藏状态,打开另一个工作簿,如果单击“closures”button,则两个工作簿都将尝试closures。

我已经尝试创build一个具有应用程序级事件跟踪器的类模块,以便监视所有工作簿的closures事件。 但我的问题是,当我单击closuresbutton时,试图closures的第一个工作簿是隐藏的工作簿。 所以我可以捕捉closures事件,并防止隐藏工作簿closures,但如果我将Cancel设置为True ,它会阻止所有的工作簿closures!

我能想到的唯一解决方法是当用户试图closures工作簿时,取消closures事件并取消隐藏隐藏的工作簿。 但是我不知道如何识别用户试图closures哪个工作簿 – 所以我无法自己closures正确的工作簿。

我目前设置了WorkbookBeforeClose事件,如下所示:

 Public WithEvents A As Excel.Application Private Sub A_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean) If Workbooks.Count > 1 Then If Windows(ThisWorkbook.Name).Visible = False Then If Wb.Name = ThisWorkbook.Name Then Cancel = True End If End If End If End Sub 

如果我单步执行此代码,则会发现Wb.Name是电话营销工作簿的名称(即使它是隐藏的),而用户实际尝试closures的工作簿的名称根本不显示我可以解决

任何人都可以进一步的build议吗?

另外要提到的是,它需要在Excel 2013和Excel 2010上运行。

对不起,我很快就回答我自己的问题。 这表明我没有做足够的研究预先。 但是,对于有类似问题的人来说,这是我的解决scheme。 这个代码需要被张贴在一个类模块中,当然这个类的一个实例需要被创build才能被使用。

注意:在下面的例子中,“TT”涉及电话营销跟踪器

 Public WithEvents A As Excel.Application Private Sub A_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean) Dim VIS As Boolean, myAW As Workbook If Workbooks.Count > 1 Then 'if there is more than one workbook open... If Windows(ThisWorkbook.Name).Visible = False Then 'and if TT is invisible... If ActiveWorkbook.Name = ThisWorkbook.Name Then 'and if the workbook being closed is the TT. Windows(ThisWorkbook.Name).Visible = True Else 'more than one wb open, TT is invisible, and the workbook being closed is NOT the TT. Set myAW = ActiveWorkbook Cancel = True Windows(ThisWorkbook.Name).Visible = True Application.EnableEvents = False myAW.Close Application.EnableEvents = True If TelesalesForm.HideUnhideButton.Tag = "False" Then 'NB: I use a tag on the Hide/Unhide button on the UserForm to store whether the workbook should be hidden or not. If Workbooks.Count > 1 Then Windows(ThisWorkbook.Name).Visible = False Else ThisWorkbook.Application.Visible = False End If End If Exit Sub End If ElseIf ActiveWorkbook.Name <> ThisWorkbook.Name Then 'more than one workbook open and the TT is visible and the workbook being closed is NOT the TT Exit Sub End If End If 'code gets to this point ONLY under the following circumstances: 'There is only one workbook open (ie the TT) OR 'There is more than one WB open and the WB being closed is the TT. 'The rest of the code goes here for managing the closing of the TT. End Sub 

如果有人想到代码的其他装饰或改进,那么我会很高兴听到他们的声音!