Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Case not recognized in Select Case

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Case not recognized in Select Case

On Mon, 29 Mar 2010 08:52:02 -0700, 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?


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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default Case not recognized in Select Case

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?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Case not recognized in Select Case

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
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?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default Case not recognized in Select Case

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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
change data of entire column from small case to upper case Ann Excel Worksheet Functions 1 August 16th 08 01:06 PM
Case without Select Case error problem Ayo Excel Discussion (Misc queries) 2 May 16th 08 03:48 PM
Changing multiple cell text from lower case to upper case Patti Excel Discussion (Misc queries) 2 January 4th 08 09:35 PM
Change the text from lower case to upper case in an Excel work boo dave01968 Excel Discussion (Misc queries) 2 December 9th 05 10:09 AM


All times are GMT +1. The time now is 12:51 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"