簡體   English   中英

如何使用輸入框刪除多個工作表

[英]How to delete multiple sheets using input box

只是需要一些幫助。 我想使用我將在輸入框中輸入的部分名稱刪除多張工作表。 有什么代碼可以讓我在輸入框中添加多個部分名稱以便立即刪除它們嗎?

例如,我想添加這些部分名稱:“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 中,工作表就會被刪除。

這是一種捕獲短語列表的方法。 輸入文本必須具有您編碼的通用分隔符。 在這種情況下,我使用了分號。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM