excel - 有没有办法检查不可见的打开的 Excel 工作簿?

标签 excel vba

我有一个打开的工作簿,其中带有“保存关闭”按钮。我希望该按钮上的事件能够关闭整个 Excel 应用程序(如果它是最后打开的工作簿)。问题是,可能会打开未知的 individual.xlsb 或打开任何其他不可见的宏工作簿。

我想知道是否有其他可用的 Excel 工作簿打开。是否检查不可见的工作簿?如果是最后一个工作簿,请关闭 Excel 应用程序,如果不是关闭事件工作簿,这就是我得到的结果:

Sub CloseForceSave()

'Save the workbook.
ThisWorkbook.Save

'If this is the only workbook opened in the current session, then...
If Workbooks.Count = 1 Then "or Workbooks.Count = 2" to account for personal.xlsb
'...quit Excel
Application.Quit
'Else...
Else
'...close the active workbook
ThisWorkbook.Close
End If

End Sub

最佳答案

当我这样做时,我只是忽略任何隐藏的工作簿。如果只有一本带有可见窗口的工作簿,我会退出该应用程序。我不知道有什么比循环更好的计数方法,但我从来没有打开足够的工作簿,因此这会成为性能问题。

Function VisibleWorkbookCount() As Long

    Dim wb As Workbook
    Dim wd As Window
    Dim lReturn As Long

    For Each wb In Application.Workbooks
        For Each wd In wb.Windows
            If wd.Visible Then
                lReturn = lReturn + 1
                Exit For
            End If
        Next wd
    Next wb

    VisibleWorkbookCount = lReturn

End Function

关于excel - 有没有办法检查不可见的打开的 Excel 工作簿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38644838/

相关文章:

excel - 在 VBA 代码中压缩多个 OR 条件

ms-access - VBA:词频数组

excel - 如何在 Microsoft Excel 2007 中自动进行 Web 查询登录以下载数据?

vba - excel vba,添加多个.To电子邮件地址

c - 在 linux 中通过 C 访问 excel 表

python - xlwings 与 openpyxl 阅读 Excel 工作簿的区别

excel - 从 Excel 中打开 Word 文档(Office 2016 和 MacOS)

excel - #NA 值的清理 Excel

excel - 为什么宏给出编译错误而脚本中没有任何逻辑或语法错误?

sql - 从 Power Pivot ("Item.data"提取 2000 万行)