【问题标题】:Excel 2013 - issue when closing multiple workbooks if one workbook is hiddenExcel 2013 - 如果隐藏一个工作簿,则关闭多个工作簿时出现问题
【发布时间】:2015-07-16 09:21:50
【问题描述】:

我在 Excel VBA 中编写了一个程序,它使用用户窗体来输入用户的数据。具体来说,它是一个 Telemarketing Tracker 工具:用户在 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 工作簿时,显然会出现某些问题。我已经解决了大多数这些问题,但有一点我似乎无法解决:如果电话营销工作簿被隐藏,另一个工作簿打开,如果我单击关闭按钮,两个工作簿都会尝试关闭。

我尝试使用应用程序级别事件跟踪器创建一个类模块,以便监视所有工作簿的关闭事件。但我的问题是,当我单击关闭按钮时,第一个尝试关闭的工作簿是 hidden 工作簿。所以我可以捕获关闭事件并阻止隐藏的工作簿关闭,但如果我将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 是电话营销工作簿的名称(即使它是隐藏的)以及用户实际上尝试使用的工作簿的名称close 根本没有出现 - 据我所知。

任何人都可以提出任何进一步的建议吗?

我应该提到的另一件事是它需要在 Excel 2013 和 Excel 2010 上运行。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    很抱歉这么快就发布了我自己的问题的答案。这有点表明我事先没有做足够的研究。但是,对于任何有类似问题的人,这是我的解决方案。当然,此代码需要发布在类模块中,并且需要创建类的实例才能工作。

    注意:在下面的示例中,“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
    

    如果有人想到对代码进行任何其他修饰或改进,那么我会很高兴听到他们的消息!

    【讨论】:

    • 也许是矫枉过正?我只是使用 For each wb in workbooks, if name TT, close
    • @findwindow 感谢您的回复。也许我说得不够清楚。如果打开了两个工作簿,您的方法应该会很好地工作。但是,如果用户打开不止一个额外的工作簿,您的代码将尝试关闭除 TT 之外的 所有 个工作簿,而不仅仅是用户尝试关闭的工作簿。
    猜你喜欢
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多