VBA I need help with my code that deletes sheets

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
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:

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?

1666686717408.png


1666686740358.png
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi Kra,

code works like expected for me in my test.

Ciao,
Holger
 
Upvote 0
Hi Kra,

on my test I created a new workbook, creating a couple of new sheets, renaming some (one as the sampe you showed "PP uuu"). I ran the test with entering PP and the one sheet was gone. Any new sheets inserted are called Tabelle & i for me so I ran a test with e and all but one were deleted (one had been renamed to aaa).

I would take some precautions as entering and asterisk should delete any sheet in the workbook but there must be at least one sheet being left in the workbook.

even more confused why it is not working for me

Best for me in cases like that which I encountered more than once was to go out for a walk or set the new date for working on the problem to be the next day, in other words do something else that has little (idealy nothing) to do with Excel. Sometimes it is spending some time and a solution will show up. As you have not reported any error messages most of what I can think of to be the reason for not deleting sheets is not fitting here, sorry.

Holger
 
Upvote 0
Is the code in the same workbook as the sheets to be deleted?
 
Upvote 0
Solution
Is the code in the same workbook as the sheets to be deleted?
I see what you are pointing, I changed ThisWorkbook to ActiveWorkbook and it works!

Thank you Fluff!
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top