Solved

Delete worksheets based on a for next loop not working

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

14 Experts available now in Live!

Get 1:1 Help Now