excel - 如何使用VBA删除工作簿中的空白工作表?

标签 excel vba

Sub delete()
    Dim sh As Worksheet, wb As String, c As Range
    wb = InputBox("work book name")
    Set sh = Workbooks(wb).Sheets
        For Each Sheet In sh
            If IsEmpty(sh.UsedRange) Then
            sh.delete
            End If
        Next
End Sub

我无法使用上面的代码删除空表。

最佳答案

以下代码删除当前打开的工作簿中的所有空工作表

试试这个

Sub delete()
    Application.DisplayAlerts = False
    Dim sh As Worksheet
    For Each sh In Sheets
        If IsEmpty(sh.UsedRange) Then sh.delete
    Next
    Application.DisplayAlerts = True
End Sub

如果您想使用名称指定完整路径

Sub delete()
    Dim wb As Workbook, s As String
    s = InputBox("Full workbook path & name")

    Dim fileExists As Boolean
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fileExists = fso.fileExists(s)

    If fileExist Then
        Set wb = Workbooks.Open(s)
        For Each Sheet In sh
            If IsEmpty(sh.UsedRange) Then
            sh.delete
            End If
        Next
    Else
        MsgBox "File doesn't exist", vbCritical, "Error"
    End If

End Sub

关于excel - 如何使用VBA删除工作簿中的空白工作表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18594223/

相关文章:

VBA - EXCEL 删除指定范围以外的列

excel - 如何在 Excel 中将每行的 URL ID 加 1?

sql - 通过 "caching"表 Access 提高DLookup的速度还是其他策略?

excel - 从 Word 到 Excel 的 VBA

excel - 跳到循环 vba 中的下一次迭代

excel - 具有行总和的 COUNTIF 条件

excel - Microsoft.ACE.OLEDB.12.0 当前记录集不支持更新尝试更新 Access 时收到的错误

python - 使用 Python 循环遍历 Excel 工作表并将值存储在变量中

vba - 在 Excel VBA 中设置默认 TextBox.Value TypeName

excel - 将 FormulaArray 求值保存到实际数组