【问题标题】:Excel Automation: Identifying and deleting blank work sheetsExcel 自动化:识别和删除空白工作表
【发布时间】:2011-06-05 21:24:43
【问题描述】:

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

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

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

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

如果工作表完全为空,有什么方法可以识别和删除工作表吗?

【问题讨论】:

    标签: excel


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      你可以更彻底,遍历所有你想测试的相关对象

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-31
        • 1970-01-01
        • 2011-11-12
        • 1970-01-01
        • 2018-01-04
        • 1970-01-01
        相关资源
        最近更新 更多