R
RLN
RE: 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>
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>