2012-08-24 30 views
3

我怎樣才能刪除其中類似刪除表,其中名稱,如

Left(SheetExists.Name, 16) = "Mgt Report as at" 

表名試圖片:

Sheets(Left(SheetExists.Name, 16) = "Mgt Report as at").Delete 

回答

12

像這樣(未測試):

For Each s in ActiveWorkbook.Sheets 
    If Left(s.Name, 16) = "Mgt Report as at" Then 
     s.Delete 
    End If 
Next s 
+0

+ 1作爲OP想要的。 –

4

另一個正如你在問題標題中提到的那樣使用LIKE

另請注意,在這種情況下,您在刪除工作表時必須小心。請參閱下面代碼中的註釋。

Option Explicit 

Sub Sample() 
    Dim ws As Worksheet 

    For Each ws In ThisWorkbook.Sheets 
     If ws.Name Like "Mgt Report as at" & "*" Then 
      '~~> This check is required to ensure that you don't get an error 
      '~~> if there is only one sheet left and it matches the delete criteria 
      If ThisWorkbook.Sheets.Count = 1 Then 
       MsgBox "There is only one sheet left and you cannot delete it" 
      Else 
       '~~> This is required to supress the dialog box which excel shows 
       '~~> When you delete a sheet. Remove it if you want to see the 
       '~~~> Dialog Box 
       Application.DisplayAlerts = False 
       ws.Delete 
       Application.DisplayAlerts = True 
      End If 
     End If 
    Next 
End Sub