2010-08-25 77 views
3

如果工作表不包含數據/圖表/圖像/繪圖/超鏈接對象或任何其他嵌入對象,我想刪除它。Excel自動化:識別和刪除空白工作表

我發現的檢測和刪除空白紙上的解決方案,如果沒有在細胞中沒有數據通過使用以下代碼: -

如果($ Worksheet_Function-> COUNTA($片 - > {細胞}) == 0){ $ sheet-> Delete; }

但它也刪除工作表,如果有圖表或非文本對象。

如果紙張完全空白,是否有任何方法來識別和刪除紙張?

回答

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

你可以更徹底,更要經歷所有你要測試的相關對象

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