3

如何删除工作表名称喜欢的工作表

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

试过:

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

2 回答 2

12

像这样的东西(未经测试):

For Each s in ActiveWorkbook.Sheets
    If Left(s.Name, 16) = "Mgt Report as at" Then
        s.Delete
    End If
Next s
于 2012-08-24T07:07:05.830 回答
5

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
于 2012-08-26T08:29:51.880 回答