Solved

Delete worksheets based on a for next loop not working

Posted on 2007-11-19
5
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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