LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Deleting Sheets via Macro

Excel 2003 SP3 / WinXP SP3

I have an Excel File that has 5 sheets: NewSheet1, NewSheet2,
NewSheet3, NewSheet4, and Sheet1.
Only 'Sheet1' one contains data, and in this case, the other four do
not. So I want to delete the other four sheets.

Problem is, after 'NewSheet1' is deleted, it jumps to 'NewSheet3' and
asks to delete that one an appears to skip over 'NewSheet2'. I need
to have the other four deleted if they do not contain any data. I've
been trying to debug this one for quite a while, and cannot see what
is wrong with the code. If there is another way to do this to delete
the empty sheets only, a code sample would be helpful.

Thanks.

Here is my code currently: (indented from original layout)
<begin code
Sub DeleteExtraSheets()
' Excel Macro - coded to run inside of an Excel file.
' DeleteExtraSheets Macro
' Macro recorded 5/5/2009

Dim strMacroTitle As String
Dim SheetObject As Object
Dim x As Integer
Dim strMsgYes As String 'used for concatenation based on
condition
Dim strMsgNo As String
Dim intMsgResult 'dimmed as Variant so we can use vbYes/vbNo

strMacroTitle = "Excel Macro-DeleteExtraSheets"
For Each SheetObject In ActiveWorkbook.Sheets
x = x + 1
Sheets(Sheets(x).Name).Select 'select this sheet for deletion
prior to possible deletion
intMsgResult = MsgBox("Delete this sheet: '" & Sheets
(x).Name & "'", vbYesNo, strMacroTitle)

Select Case intMsgResult
Case vbYes
ActiveWindow.SelectedSheets.Delete
strMsgYes = strMsgYes & "Sheet " & "'" & Sheets
(x).Name & "' was deleted." & vbCrLf

Case vbNo
strMsgNo = strMsgNo & "Sheet " & "'" & Sheets
(x).Name & "' was not deleted." & vbCrLf

Case Else
'do nothing here
End Select

Next SheetObject

MsgBox strMsgYes & vbCrLf & strMsgNo, vbOKOnly, strMacroTitle

End Sub

<end code

 
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
Deleting Custom Views when Deleting Sheets ExcelMonkey Excel Programming 1 March 28th 07 06:11 AM
Deleting Sheets oakman[_34_] Excel Programming 4 August 15th 06 03:43 PM
Deleting all sheets except one Nirmal Singh[_2_] Excel Programming 2 January 26th 06 03:57 PM
Deleting Sheets ExcelMonkey[_190_] Excel Programming 1 March 25th 05 10:02 AM
deleting sheets KLP Excel Discussion (Misc queries) 3 January 14th 05 12:24 AM


All times are GMT +1. The time now is 04:26 PM.

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

About Us

"It's about Microsoft Excel"