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

时间:2013-09-03 13:57:40

标签: excel excel-vba 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

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

1 个答案:

答案 0 :(得分:5)

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

试试这个

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

如果要指定名称为use

的完整路径
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