vba 删除名称类似的工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12104524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:52:07  来源:igfitidea点击:

Delete sheet where name like

excelvbaexcel-vba

提问by HL8

How can I delete sheet where the sheet name like

如何删除工作表名称类似的工作表

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

Tried:

尝试:

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

回答by assylias

Something like this (not tested):

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

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

回答by Siddharth Rout

Another way using LIKEas you have mentioned in the title of your question.

LIKE您在问题标题中提到的另一种使用方式。

Also note, in such a scenario, you have to be careful when you are deleting the sheets. See the comments in the code below.

另请注意,在这种情况下,删除工作表时必须小心。请参阅下面代码中的注释。

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