Macro to delete a sheet if empty - error...

jillian728

Active Member
Joined
Jun 20, 2003
Messages
250
We are using a macro that looks if ranges w/in sheets are blank and if yes -they are deleted. However, sometimes a user might delete the sheet b4 the macro is run. How can we tweak the macro so that if the sheet is found it is deleted and if not found, it doesn't delete it and doesn't cause an error.



If Application.Evaluate("=MAX(LEN('Sys Matrix'!D5:D62))") = 0 Then

Application.DisplayAlerts = False

Sheets("SYS Matrix").Delete

Application.DisplayAlerts = True

End If

If Application.Evaluate("=MAX(LEN('SW Mtx'!D5:D62))") = 0 Then

Application.DisplayAlerts = False

Sheets("SW Mtx").Delete

Application.DisplayAlerts = True

End If
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Code:
Private Function SheetExists(sname) As Boolean
    Dim x As Object
    On Error Resume Next
    Set x = ActiveWorkbook.Sheets(sname)
    If Err = 0 Then SheetExists = True _
        Else SheetExists = False
End Function

You could add this function to the end of your macro and within your code place:

Code:
If SheetExists ("placeyoursheetnamehere") = true then

'place your delete code here

else

'what to do if the sheet doesn't exist

end if

I forget where I came across this function, but it's been quite useful.
 
Upvote 0
How about something like this....it will ignore your hidden sheets but will delete all others in the workbook whose range D5:D62 is blank...

Code:
Sub myEvalShts()

Dim mySht As Variant

Application.DisplayAlerts = False

For Each mySht In ActiveWorkbook.Sheets
    If mySht.Visible = True Then
        mySht.Activate
        If Application.Evaluate("=MAX(LEN(D5:D62))") = 0 Then
            mySht.Delete
        End If
    End If
Next
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Thanks for all of these great suggestions. We will try them out when we return from holiday break and post the results!
 
Upvote 0
Thanks again everyone!

We used Andrew's suggestion and added the words On Error Resume Next to the beginning of the code and this worked perfectly!
 
Upvote 0

Forum statistics

Threads
1,207,261
Messages
6,077,356
Members
446,279
Latest member
hoangquan2310

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