2016-12-27 25 views
0

我做了一個代碼,刪除工作表,如果範圍「D14:K70」是空的整個工作簿,但我不知道如何忽略「---------- - 「,它們在工作表範圍內72x和空的空格是368x。我也是有一個問題在表(I).Delete刪除工作表,如果範圍爲空

Sub DeletingEmptyPages() 

Dim i As Long 

For i = Sheets.count To 1 Step -1 
    If WorksheetFunction.CountIf(Sheets(i).Range("D14:K70"), "") >= 368 Then 
     If Sheets.count > 1 Then 
      Application.DisplayAlerts = False 
      Sheets(i).Delete 
      Application.DisplayAlerts = True 
     Else 
      MsgBox "Only 1 sheet left" 
      Exit Sub 
     End If 
    End If 
Next i 
End Sub 
+0

使用'CountA'代替 –

+0

@ShaiRado我做到了,COUNTA刪除所有表 – MTBthePRO

+0

您是否嘗試過的IsEmpty呢?您可以將它與替換爲「 - 」結合使用。 – Liss

回答

0
Sub DeletingEmptyPages() 

Dim i As Long 

For i = Sheets.count To 1 Step -1 
    If WorksheetFunction.CountA(Sheets(i).Range("D14:K25, D27:K27, D29:K31, D34:K34, D36:K52, D55:K55, D57:K57, D59:K66, D68:K68, D70:K70 "), "") >= 368 Then 
     If Sheets.count > 1 Then 
      Application.DisplayAlerts = False 
      Sheets(i).Delete 
      Application.DisplayAlerts = True 
     Else 
      MsgBox "Only 1 sheet left" 
      Exit Sub 
     End If 
    End If 
Next i 
End Sub 
0

稍有不同逼近同一問題的方式。你可以添加一個完整的循環來停止,如果它找到了一些東西,因爲它會稍微加快這個過程,但範圍很小,我不認爲這會是一個問題。

Sub DeletingEmptyPages() 

Dim i As Long 
Dim strTestRange As String 
Dim bDeleteSheet As Boolean 

strTestRange = "D14:K70" 

For Each ws In ThisWorkbook.Worksheets 
    bDeleteSheet = True 
    For Each c In ws.Range(strTestRange) 
     If Not (c.Value = "" Or c.Value = "-") Then bDeleteSheet = False 
    Next c 

    If bDeleteSheet Then 
     If Sheets.Count > 1 Then 
      Application.DisplayAlerts = False 
      ws.Delete 
      Application.DisplayAlerts = True 
     Else 
      MsgBox "Only 1 sheet left" 
      Exit Sub 
     End If 
    End If 

Next ws 

End Sub