删除名称之类的表格

HL8*_*HL8 3 excel vba excel-vba

如何删除工作表名称所在的工作表

Left(SheetExists.Name, 16) = "Mgt Report as at"
Run Code Online (Sandbox Code Playgroud)

尝试:

Sheets(Left(SheetExists.Name, 16) = "Mgt Report as at").Delete
Run Code Online (Sandbox Code Playgroud)

ass*_*ias 12

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

For Each s in ActiveWorkbook.Sheets
    If Left(s.Name, 16) = "Mgt Report as at" Then
        s.Delete
    End If
Next s
Run Code Online (Sandbox Code Playgroud)


Sid*_*out 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
Run Code Online (Sandbox Code Playgroud)