简体   繁体   English

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

[英]How to delete a blank sheet in a workbook using 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

I am unable to delete the empty sheets using above code. 我无法使用上述代码删除空白表。

The below code deletes all empty sheets in the currently opened workbook 下面的代码删除当前打开的工作簿中的所有空白工作表

try this instead 试试这个代替

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

if you want to specify the full path with the name 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM