Find sheet in wb with certain word and delete

Lizard07

Board Regular
Joined
Jul 20, 2011
Messages
103
I would like to create a Macro that goes through each sheet in my workbook (except the sheets: "Control Panel" and "Master") and finds the word "Hook: #####". Then, if that sheet contains the word hook I would like the macro to then delete it.

There is another condition. I only want it to delete sheets that have the word "Hook:" with numbers after it, NOT text. Example

Hook: 78945757 - Delete this sheet
Hook: Unknown - do not delete this sheet

Below is all I have right now, I'm not very good with loop codes - please help!

Sub FindTrailersandDelete()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
' Loop through all worksheets and delete ones with a trailer #
For Each sh In ActiveWorkbook.Worksheets
On Error Resume Next
Sheets(1).Rows.Find(what:="Hook:", lookat:=xlPart).EntireSheet.Delete
On Error GoTo 0
Next
Application.DisplayAlerts = True
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Well first, a sheet name cannot contain a :
So I assume you mean just Hook, not Hook:


Try this. PLEASE Save your book first!!!!!

Code:
Sub FindTrailersandDelete()
Dim ws As Worksheet
With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With
 
For Each ws In Sheets
    If Left(ws.Name, 4) = "Hook" Then
        If Not IsNumeric(Right(ws.Name, Len(ws.Name) - 4)) Then
            ws.Delete
        End If
    End If
Next ws
 
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
End Sub
 
Upvote 0
Its not the sheet name that contains "Hook:", its if there are cells within the worksheets that contain "Hook:"
 
Upvote 0
Its not the sheet name that contains "Hook:", its if there are cells within the worksheets that contain "Hook:"

Ahh, OK..

will it appear ANYWHERE in the sheet, or just in row 1 as your posted code seems to be doing
 
Upvote 0
It will actually be located in COLUMN 8 - Thank-you for your help, sorry for the miscommunication here
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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