Delete Worksheet if 'text' is found within it

leeksleeks

Board Regular
Joined
Oct 31, 2013
Messages
96
Hi,

I would like a macro to delete my worksheet called 'Scorecard' if the word 'abandoned' is found within it. The current macro imports web data but if it imports one with the word 'abandoned' on it I would like the whole worksheet (Scorecard) to be deleted.

Let me know if you need anymore information.

Cheers
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi,

I would like a macro to delete my worksheet called 'Scorecard' if the word 'abandoned' is found within it. The current macro imports web data but if it imports one with the word 'abandoned' on it I would like the whole worksheet (Scorecard) to be deleted.

Let me know if you need anymore information.

Cheers
Assuming there is at least one sheet besides the Scorecard sheet in your workbook:
Code:
Sub DeleteIfAbandoned()
Const fWhat As String = "abandoned"
Dim fR As Range
If ActiveWorkbook.Sheets.Count = 1 Then Exit Sub
With ActiveWorkbook.Sheets("Scorecard").UsedRange
    Set fR = .Find(fWhat, .Cells(1, 1), xlFormulas, xlPart)
    If Not fR Is Nothing Then
        Application.DisplayAlerts = False
        Sheets("Scorecard").Delete
        Application.DisplayAlerts = True
        Exit Sub
    End If
End With
End Sub
 
Upvote 0
Assuming there is at least one sheet besides the Scorecard sheet in your workbook:
Code:
Sub DeleteIfAbandoned()
Const fWhat As String = "abandoned"
Dim fR As Range
If ActiveWorkbook.Sheets.Count = 1 Then Exit Sub
With ActiveWorkbook.Sheets("Scorecard").UsedRange
    Set fR = .Find(fWhat, .Cells(1, 1), xlFormulas, xlPart)
    If Not fR Is Nothing Then
        Application.DisplayAlerts = False
        Sheets("Scorecard").Delete
        Application.DisplayAlerts = True
        Exit Sub
    End If
End With
End Sub

You can simplify your code slightly by using the worksheet function's COUNTIF function instead of the range's Find function (I'm guessing the total amount of data on a scorecard would be small enough that any time difference between your method and mine would be negligible)...

Code:
Sub DeleteIfAbandoned()
  Const fWhat As String = "abandoned"
  If ActiveWorkbook.Sheets.Count = 1 Then Exit Sub
  With ActiveWorkbook.Sheets("Scorecard").UsedRange
    If Application.CountIf(.Cells, fWhat) Then
      Application.DisplayAlerts = False
      Sheets("Scorecard").Delete
      Application.DisplayAlerts = True
      Exit Sub
    End If
  End With
End Sub
 
Upvote 0
You can simplify your code slightly by using the worksheet function's COUNTIF function instead of the range's Find function (I'm guessing the total amount of data on a scorecard would be small enough that any time difference between your method and mine would be negligible)...

Code:
Sub DeleteIfAbandoned()
  Const fWhat As String = "abandoned"
  If ActiveWorkbook.Sheets.Count = 1 Then Exit Sub
  With ActiveWorkbook.Sheets("Scorecard").UsedRange
    If Application.CountIf(.Cells, fWhat) Then
      Application.DisplayAlerts = False
      Sheets("Scorecard").Delete
      Application.DisplayAlerts = True
      Exit Sub
    End If
  End With
End Sub
As it turns out, I had just replied to another post that needed the Range.Find Method and it was easiest (meaning less typing) to take the existing code and modify it. I use the worksheet function Countif frequently in VBA and find it to be vary fast so I agree your approach would produce comparable results.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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