Solved

Unhiding and deleting multiple sheets based on their names

Posted on 2011-09-07
4
207 Views
Last Modified: 2012-08-14
I get a type mismatch error on line 3 in the code below. What is the proper way to write that line?

Thanks,
John
Sub RemoveSheets()
Dim i As Long, ws As Worksheet
For i = 1 To ActiveWorkbook.Worksheets.Count
    If Worksheets(i).Name = "ACV" Or "B773i4" Or "JETZ" Or "LEGACY" Then
    Worksheets(i).Visible = True
    Worksheets(i).Delete
    End If
Next
End Sub

Open in new window

0
Comment
Question by:gabrielPennyback
4 Comments
 
LVL 7

Expert Comment

by:BusyMama
ID: 36498782
I think if you Dim i as Integer?
0
 
LVL 10

Accepted Solution

by:
SANTABABY earned 500 total points
ID: 36498822
Your code at Line 4 does not have not correct syntax.
Replace with

If Worksheets(i).Name = "ACV" Or Worksheets(i).Name = "B773i4" Or Worksheets(i).Name = "JETZ" Or Worksheets(i).Name = "LEGACY"  Then

Open in new window


0
 
LVL 6

Expert Comment

by:reitzen
ID: 36498841
Unless you need to know the sheet number, it would be better to loop over the worksheets collection.


dim ws as worksheet
for each ws in worksheets
    if ws.name = "ACV" then
        ...
    end if
next ws

Open in new window

0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 36499790
I could swear I tried exactly what you posted, but obviously not exactly :-)  Thanks, SantaBaby.

- John
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

791 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