Deleting Sheets WITHOUT having to click confirm...

GS7CLW

Banned
Joined
Aug 10, 2010
Messages
168
Using this code to delete sheets I no longer want to include in my wb; but, I still have to click the confirm msg box that pops up for every sheet...

Code:
Sub DeleteOldSheets()
Dim Worksheet
Dim RSID As String
Dim WS_Count As Integer
   Dim I As Integer
   ' Set WS_Count equal to the number of worksheets in the active
   ' workbook.
   WS_Count = ActiveWorkbook.Worksheets.Count
   ' Begin the loop.
   For I = 1 To WS_Count
    ActiveWorkbook.Worksheets(I).Select
    RSID = ActiveSheet.Name
    Sheets(RSID).Select
    If Len(RSID) < 5 Then
    ActiveWindow.SelectedSheets.Delete
    End If
   Next I
End Sub

any way around it??

hope all are having a great one -- HUMP DAY!!!!
cliff
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try turning off DisplayAlerts:

Code:
Application.DisplayAlerts = False
Sheets("sheet name goes here").Delete
Application.DisplayAlerts = True
 
Upvote 0
Just add this two lines
Code:
Sub DeleteOldSheets()
Dim Worksheet
Dim RSID As String
Dim WS_Count As Integer
   Dim I As Integer
   ' Set WS_Count equal to the number of worksheets in the active
   ' workbook.
[B][U]Application.DisplayAlerts = False[/U][/B]
  WS_Count = ActiveWorkbook.Worksheets.Count
   ' Begin the loop.
   For I = 1 To WS_Count
    ActiveWorkbook.Worksheets(I).Select
    RSID = ActiveSheet.Name
    Sheets(RSID).Select
    If Len(RSID) < 5 Then
    ActiveWindow.SelectedSheets.Delete
    End If
   Next I
[B][U]Application.DisplayAlerts = True[/U][/B]

End Sub
 
Upvote 0
works perfectly.

And after I got past that issue another "popped up"...

It stops and highlights:
ActiveWorkbook.Worksheets(I).Select

which I am sure is because the I (WS_Count) changes after each sheet is deleted. So, I believe I need to increment I backwards -- correct?
 
Upvote 0
Code:
For I = WS_Count To 1 Step -1
 
Upvote 0
Absolutely!!!!

I really should have remembered that but it has been over a decade since I used VB\A.

THANK YOU!!!!!!!!!!!!!!

cliff
 
Upvote 0
Personally I always cycle through the WorkSheets collection and then I don't need to worry about the order of the worksheets changing mid-processing.

Your code appears to be selecting each sheet by its number, storing its name, then selecting it again by its name and deleting it if its name is less than five characters in length. Have I got it right?

If so, I think you just need this:-
Code:
  Dim ws As WorkSheet
 
  For Each ws In ThisWorkbook.WorkSheets
    If Len(ws.Name) < 5 Then
      Application.DisplayAlerts = False
      ws.Delete
      Application.DisplayAlerts = True
    End If
  Next ws
 
Upvote 0

Forum statistics

Threads
1,224,593
Messages
6,179,791
Members
452,942
Latest member
VijayNewtoExcel

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