Hi all,
I am trying to create code that will delete all sheets in active workbook if name contains part text from input. So far nothing seems working and I have no cue why,
I am using this code:
But when I enter specific text (no spaces before or after) it doesn't work. Any ideas how to fix it?
I am trying to create code that will delete all sheets in active workbook if name contains part text from input. So far nothing seems working and I have no cue why,
I am using this code:
VBA Code:
Private Sub CommandButton28_Click()
Dim shName As String
Dim xName As String
Dim xWs As Worksheet
Dim cnt As Integer
shName = Application.InputBox("Enter the specific text:", "Delete sheets", _
ThisWorkbook.ActiveSheet.Name, , , , , 2)
If shName = "" Then Exit Sub
xName = "*" & shName & "*"
' MsgBox xName
Application.DisplayAlerts = False
cnt = 0
For Each xWs In ThisWorkbook.Sheets
If xWs.Name Like xName Then
xWs.Delete
cnt = cnt + 1
End If
Next xWs
Application.DisplayAlerts = True
MsgBox "Have deleted " & cnt & " worksheets", vbInformation, "Sheets removed"
End Sub
But when I enter specific text (no spaces before or after) it doesn't work. Any ideas how to fix it?