Solved

Delete worksheets based on a for next loop not working

Posted on 2007-11-19
5
476 Views
Last Modified: 2013-11-25
I have the below, I have an Excel workbook that has up to 25 worksheets.  The code brings in information from a database and depending on the product, anywhere from 1 to 25 of the worksheets are used, that is, the code changes the name of hte worksheet from just, say, "1" to "Lipstick".  However, if not all of the worksheets are used, I need to delete those that still have their "number".  The sheets are named 1 to 25 before being used (I did not set up this workbook, it is inherited)  So.  I need to stream line and delete the extra workbooks, but I simply cannot get the below to work.  I keep getting a subscript out of range error message.

Public Sub DeleteUnused()
Dim ws As Worksheet, i As Integer
i = 25
 
For i = 25 To 1 Step -1
    If Worksheets(i).Name = Worksheets(" & i & ") Then
        Worksheets(" & i & ").Delete
    End If
Next i
    
End Sub

Open in new window

0
Comment
Question by:ssmith94015
5 Comments
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 100 total points
ID: 20316868
Public Sub DeleteUnused()
Dim ws As Worksheet, i As Integer
i = 25

On Error Resume Next
Application.DisplayAlerts = False
For i = 25 To 1 Step -1
    Worksheets(" & i & ").Delete
Next i
On Error GoTo 0
Application.DisplayAlerts = True

End Sub

Kevin
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 100 total points
ID: 20316886
Note:
When you delete a Worksheet, this method displays a dialog box that prompts the user to confirm the deletion. This dialog box is displayed by default. When called on the Worksheet object, the Delete method returns a Boolean value that is False if the user clicked Cancel on the dialog box or True if the user clicked Delete.

Public Sub DeleteUnused()
Dim ws As Worksheet, i As Integer
 
For i = Worksheets.Count To 1 Step -1
    MsgBox Worksheets(i).Name
    If Worksheets(i).Name = LTrim(RTrim(Str(i))) Then
        Worksheets(i).Delete
    End If
Next i
 
End Sub

Open in new window

0
 
LVL 1

Accepted Solution

by:
bkenny_ie earned 300 total points
ID: 20316894
Hiya,
Not sure I follow the (" & i & ") bit, but if the sheets are just numbered you could use the below. Regardless, the worksheets collection is the better way to go. My bet is you've got less than 25 in the book, hence the out of range error.

cheers,
Brian

Sub DelSheets()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If IsNumeric(ws.Name) = True Then
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        End If
    Next ws
End Sub

Open in new window

0
 

Author Comment

by:ssmith94015
ID: 20316910
Wow!  All three were good and worked!  So I am going to split the points, but give the larger portion to Brian as this was the one I actually used.  Thank you all!
0
 
LVL 1

Expert Comment

by:bkenny_ie
ID: 20316935
good man - cheers!

B
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question