Excel自动化:识别和删除空白工作表

时间:2010-08-25 12:09:49

标签: excel

我想删除工作表,如果它不包含数据/图表/图像/绘图/超链接对象或任何其他嵌入对象。

如果单元格中没有数据,我会找到使用以下代码检测和删除空白工作表的解决方案: -

if($ Worksheet_Function-> CountA($ sheet-> {Cells})== 0){      $片状>删除; }

但如果有图表或非文本对象,它也会删除工作表。

如果纸张完全为空,有没有办法识别和删除纸张?

2 个答案:

答案 0 :(得分:1)

如果只有格式化,

将删除工作表,但这应该按照您的要求进行

Sub chksheet()
 Dim wks As Worksheet
 Application.DisplayAlerts = False

 For Each wks In ActiveWorkbook.Worksheets

 If WorksheetFunction.CountA(Cells) = 0 And wks.DrawingObjects.Count = 0 Then
  wks.Delete
  Else
   MsgBox ("has stuff") 'or do nothing here and skip this sheet
  End If
 Next wks

 Set wks = Nothing
  Application.DisplayAlerts = True
 End Sub

答案 1 :(得分:1)

您可以更彻底地浏览所有要测试的相关对象

Option Explicit

Sub test()
Dim WS As Worksheet

For Each WS In ThisWorkbook.Worksheets
    With WS
        'default usedrange = 1 so check cell A1 is also empty
        If .UsedRange.Count = 1 And IsEmpty(.Cells(1, 1).Value) _
            And .UsedRange.Column = 1 _
            And .UsedRange.Row = 1 _
            And .Comments.Count = 0 _
            And .Shapes.Count = 0 _
            And .Hyperlinks.Count = 0 _
            And .ListObjects.Count = 0 _
            And .OLEObjects.Count = 0 _
            And .Names.Count = 0 _
            And .QueryTables.Count = 0 _
            And .SmartTags.Count = 0 Then

            MsgBox ("BLANK")
            'WS.delete
        Else
            MsgBox ("NOT BLANK")
        End If

        End With
Next WS
End Sub