On May 5, 12:03*pm, RLN <rlntemp-...@yahoo.com> wrote:
> 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>
RLN,
The code you have written will loop through each sheet in the
workbook, moving to the right starting at the left-most sheet. I'm
not sure what you mean by the program "skipping over 'NewSheet2'"
because the loop will eventually get there. If you want the sheet
names to be in order, then you'll have to sort the sheets prior to
deletion. In terms of deleting "empty sheets," there are a few
different ways to do this based on your criteria. If your data is
always on 'Sheet1' and you always want all other sheets deleted then
you can something like what is listed below. The code assumes that
all of your sheets are worksheets.
Best,
Matthew Herbert
Sub DeleteSheets()
Dim Wks As Worksheet
Dim strSheet As String
'worksheet name to NOT delete
strSheet = "Sheet1"
For Each Wks In ActiveWorkbook.Worksheets
If Wks.Name <> strSheet Then
'turn off DisplayAlerts
Application.DisplayAlerts = False
'delete the worksheet
Wks.Delete
'turn off DisplayAlerts
Application.DisplayAlerts = True
End If
Next
End Sub
|