如何使用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 

如果你想指定名字使用的完整path

 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