【问题标题】:How to delete a blank sheet in a workbook using VBA?如何使用 VBA 删除工作簿中的空白表?
【发布时间】:2013-09-06 19:15:45
【问题描述】:
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

我无法使用上述代码删除空白工作表。

【问题讨论】:

  • 你用输入做什么?您需要打开一个指定完整路径的工作簿才能操作其数据
  • 如果任何工作簿有空白表,我们需要通过输入工作簿名称来删除它们。
  • 好的,每个工作簿的路径呢?他们在哪里?
  • 我不知道如何输入路径。
  • 或许可以关注this学习:)

标签: excel vba


【解决方案1】:

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

试试这个

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
  • 2019-12-31
  • 1970-01-01
  • 2016-11-16
  • 1970-01-01
相关资源
最近更新 更多