Solved

Delete worksheets based on a for next loop not working

Posted on 2007-11-19
5
472 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now