Ws.delete - Help

fpskidmark

Board Regular
Joined
Sep 11, 2009
Messages
139
Hi All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I'm stump on problem and I need some advice,<o:p></o:p>
The code below has been working for a while now, but today, for some odd reason, it's not working.<o:p></o:p>
I have spreadsheet that has 16 sheets with range (S1) populated, meaning all 16 sheets should be populated, but for some reason only 14 sheet are displayed. The last 2 sheets "15", "16" were deleted.<o:p></o:p>
Note: Both deleted sheets had a value on range (S1).<o:p></o:p>
Please help!
<o:p></o:p>
PHP:
Dim ws As Worksheet
Application.DisplayAlerts = False
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Summary" And ws.Range("S1") = "" Then ws.Delete
    Next ws
Application.DisplayAlerts = True

Thanks,
FP
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Your code looks ok. Could S1 have a formula in it that returns ""? If not, it seems S1 was empty in those sheets. You could add a warning to your code:
Code:
If ws.Range("S1") = "" Then
   msgbox "S1 is empty on sheet " & ws.name &  vbnewline & "Delete?",   vbYesNo
'rest of code
 
Upvote 0
Thanks JoeMo,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
That code looks good, but this macro is designed to run for 10 different employees so I rather not have 10 msg prompt every time we run the macro.<o:p></o:p>
Since the macro is deigned for other users to use, I rather keep it as simple as possible.<o:p></o:p>
<o:p></o:p>
I changed code to read: if S1 > 1 the ws.delete (A number should be populated in range (S1). It's running ok now:
<o:p></o:p>
PHP:
 For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Summary" And ws.Range("S1") > 1 Then ws.Delete
    Next ws
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,691
Members
452,938
Latest member
babeneker

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