PC Review


Reply
Thread Tools Rate Thread

Case not recognized in Select Case

 
 
kevlarmcc
Guest
Posts: n/a
 
      29th Mar 2010
I am using Select Case in a macro that deletes worksheets to skip
specifically named worksheets. Code is:

For Each Worksheet In Worksheets
Select Case Worksheet.name
Case "Sheet1", 'Sheet2", "Sheet3", etc.

Case Else

Worksheet.Activate
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True


End Select
Next Worksheet

The problem is I have a worksheet named "BGE" that I want skipped and the
select case is not recognizing it and deletes it. I wonder if this has to do
with Excel 2k7 using BGE as a cell value. Any ideas?
 
Reply With Quote
 
 
 
 
Ron Rosenfeld
Guest
Posts: n/a
 
      29th Mar 2010
On Mon, 29 Mar 2010 08:52:02 -0700, kevlarmcc
<(E-Mail Removed)> wrote:

>I am using Select Case in a macro that deletes worksheets to skip
>specifically named worksheets. Code is:
>
>For Each Worksheet In Worksheets
>Select Case Worksheet.name
> Case "Sheet1", 'Sheet2", "Sheet3", etc.
>
> Case Else
>
> Worksheet.Activate
> Application.DisplayAlerts = False
> ActiveSheet.Delete
> Application.DisplayAlerts = True
>
>
>End Select
>Next Worksheet
>
>The problem is I have a worksheet named "BGE" that I want skipped and the
>select case is not recognizing it and deletes it. I wonder if this has to do
>with Excel 2k7 using BGE as a cell value. Any ideas?


BGE is not in your Case statement; so when Worksheet.Name = BGE, the case else
code will get executed, and the sheet will be deleted.
--ron
 
Reply With Quote
 
Barb Reinhardt
Guest
Posts: n/a
 
      29th Mar 2010
You have 'Sheet2" instead of "Sheet2". Could that be it?
--
HTH,

Barb Reinhardt



"kevlarmcc" wrote:

> I am using Select Case in a macro that deletes worksheets to skip
> specifically named worksheets. Code is:
>
> For Each Worksheet In Worksheets
> Select Case Worksheet.name
> Case "Sheet1", 'Sheet2", "Sheet3", etc.
>
> Case Else
>
> Worksheet.Activate
> Application.DisplayAlerts = False
> ActiveSheet.Delete
> Application.DisplayAlerts = True
>
>
> End Select
> Next Worksheet
>
> The problem is I have a worksheet named "BGE" that I want skipped and the
> select case is not recognizing it and deletes it. I wonder if this has to do
> with Excel 2k7 using BGE as a cell value. Any ideas?

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      29th Mar 2010
You are using ActiveSheet.Delete which will always delete that sheet
that is active in Excel. It is NOT the sheet that is iterated by the
For Each loop. You need to either select that sheet,
(Worksheet.Select) or use the For Each variable (Worksheet.Delete). It
is a common misconception that a For Each loop activates the
ActiveSheet or ActiveCell. Such is not the case. If you expect a sheet
or cell to be active, you must activate it yourself.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Mon, 29 Mar 2010 08:52:02 -0700, kevlarmcc
<(E-Mail Removed)> wrote:

>I am using Select Case in a macro that deletes worksheets to skip
>specifically named worksheets. Code is:
>
>For Each Worksheet In Worksheets
>Select Case Worksheet.name
> Case "Sheet1", 'Sheet2", "Sheet3", etc.
>
> Case Else
>
> Worksheet.Activate
> Application.DisplayAlerts = False
> ActiveSheet.Delete
> Application.DisplayAlerts = True
>
>
>End Select
>Next Worksheet
>
>The problem is I have a worksheet named "BGE" that I want skipped and the
>select case is not recognizing it and deletes it. I wonder if this has to do
>with Excel 2k7 using BGE as a cell value. Any ideas?

 
Reply With Quote
 
Wouter HM
Guest
Posts: n/a
 
      29th Mar 2010
Hi Chip,

I think you are looking for something like:

Sub DeleteSheets()
Dim sht As Worksheet

For Each sht In ActiveWorkbook.Sheets
Select Case sht.Name
Case "Sheet1", "Sheet2", "BGE"
' Complete list of sheets to preserve
Case Else
Application.DisplayAlerts = False
sht.Delete
Application.DisplayAlerts = True
End Select
Next
End Sub


HTH,

Wouter
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
translate VB select case to switch case problem Rich P Microsoft C# .NET 13 8th Aug 2009 06:07 PM
Case without Select Case error problem Ayo Microsoft Excel Misc 2 16th May 2008 03:48 PM
Select case / case is, multiple arguments Werner Rohrmoser Microsoft Excel Programming 2 3rd Jan 2006 10:55 AM
how to case select with case-insensitive string ? Tee Microsoft ASP .NET 3 23rd Jun 2004 08:40 PM
how to case select with case-insensitive string ? Tee Microsoft VB .NET 3 23rd Jun 2004 08:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:30 AM.