[英]Excel 2013 - issue when closing multiple workbooks if one workbook is hidden
我已经在Excel VBA中编写了一个程序,该程序使用UserForm来输入用户的数据。 具体来说,它是一个电话营销跟踪工具:用户在用户窗体的文本框中填写呼叫的详细信息,然后单击相关按钮以指示该呼叫是好还是坏,然后可以继续下一个呼叫。
这些数据存储在工作表中,我们的用户通常更喜欢隐藏工作簿并仅查看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工作簿时,显然会出现某些问题。 我已经解决了大多数此类问题,但是似乎无法解决一件事: 如果电话销售工作簿处于隐藏状态,而另一个工作簿处于打开状态,如果单击“关闭”按钮,则两个工作簿都将尝试关闭。
我尝试使用应用程序级事件跟踪器创建一个类模块,以便监视所有工作簿的关闭事件。 但是我的问题是,当我单击关闭按钮时,第一个尝试关闭的工作簿就是隐藏的工作簿。 因此,我可以捕获close事件并防止关闭隐藏的工作簿,但是如果我将Cancel
设置为True
,它将阻止所有工作簿的关闭!
我能想到的唯一解决方法是,当用户尝试关闭工作簿时,我取消了“关闭事件”并取消隐藏隐藏的工作簿。 但是我不知道如何识别用户试图关闭哪个工作簿-因此我无法确定如何自动关闭正确的工作簿。
我目前已按照以下步骤设置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是电话销售工作簿的名称(即使它是隐藏的),而用户实际上试图关闭的工作簿的名称根本没有出现-到目前为止我可以锻炼。
谁能提出进一步建议?
我应该提到的另一件事是,它需要在Excel 2013和Excel 2010上运行。
很抱歉这么快就回答我自己的问题。 这表明我之前没有做足够的研究。 但是,对于有类似问题的任何人,这是我的解决方案。 当然,此代码需要发布在类模块中,并且需要创建该类的实例才能生效。
注意:在下面的示例中,“ 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 (i.e. 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
如果有人想到了代码的任何其他修饰或改进,那么我将很高兴听到它们!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.