excel 如何使用输入框删除多张工作表

zz2j4svz  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(120)

只是需要一些帮助。我想删除多个工作表使用他们的部分名称,我将在输入框中输入。有没有任何代码,我可以添加多个部分名称在输入框中,所以他们将被删除一次?
例如,我想添加以下部分名称:“Pivot”、“IWS”、“Associate”、“Split”和“Invoice”我的初始代码可以删除只有一个部分名称的工作表,示例如果我输入“Pivot”,它将删除所有具有“Pivot”名称的工作表。我想调整我的代码,我可以在输入框中添加多个部分名称。

以下是初始代码:

Sub ClearAllSheetsSpecified()
'----------------------------------------------------------------------------------------------------------
' Clear all sheets specified in input box
'----------------------------------------------------------------------------------------------------------
   Dim shName As String
   Dim xName As String
   Dim xWs As Worksheet
   Dim cnt As Integer
   shName = Application.InputBox("Enter the sheet name to delete:", "Delete sheets", _
                                   ThisWorkbook.ActiveSheet.Name, , , , , 2)
   If shName = "" Then Exit Sub
   '**** use LCase() here
   xName = "*" & LCase(shName) & "*"
'    MsgBox xName
   Application.DisplayAlerts = False
   cnt = 0
   For Each xWs In ThisWorkbook.Sheets
       '**** Use LCase() here
       If LCase(xWs.Name) Like xName Then
           xWs.Delete
           'MsgBox xName
           cnt = cnt + 1
       End If
   Next xWs
   Application.DisplayAlerts = True
   MsgBox "Have deleted " & cnt & " worksheets", vbInformation, "Sheets removed"

我正在寻找一个代码,我可以在我的输入框中输入任何部分名称,然后表将被删除,只要他们存在于我目前的WB。

qv7cva1a

qv7cva1a1#

这是一种捕获短语列表的方法。输入文本必须有一个通用的分隔符,在本例中我使用了分号。

Sub testIt()
        Dim shName As String
        Dim xName() As String
        Dim cnt As Integer
        shName = Application.InputBox("Enter the sheet names (delimited by ;) to delete:", "Delete sheets", _
                                        ThisWorkbook.ActiveSheet.Name, , , , , 2)
        If shName = "" Then Exit Sub
        '**** use LCase() here
        xName = Split(LCase(shName), ";")
        For x = 0 To UBound(xName)
                Debug.Print "*" & xName(x) & "*"
                'do your delete
        Next x
End Sub

相关问题