?
Solved

Delete worksheets based on a for next loop not working

Posted on 2007-11-19
5
Medium Priority
?
479 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 400 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 400 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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