Consulting

Results 1 to 5 of 5

Thread: Macro to Delete Blank Sheets

  1. #1
    VBAX Regular
    Joined
    Jun 2005
    Posts
    13
    Location

    Macro to Delete Blank Sheets

    Hi folks,

    Am being particularly dim this week , whilst I have stuck together a short bit of code to delete worksheets, it doesn't check to ensure they are blank.

    what I need is a bit of code that checks to see if the sheet is blank and only deletes it if it is.

    It would be really nice if it would then confirm the number of sheets deleted and list in the same message/dialogue box the sheets that were not deleted as they contained data.

    Any and all help is much appreciated

    Ross
    Last edited by rosspmm; 06-30-2005 at 04:33 AM. Reason: Additional question raised

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi Ross,

    Something like this then...

    Sub DeleteEmptyWorkSheets()
    Dim ws As Worksheet
    Dim deletedsheetcount As Long
    Dim strMessage As String
    Application.DisplayAlerts = False
        For Each ws In ActiveWorkbook.Worksheets
            If Application.CountA(ws.Cells) = 0 Then
                ws.Delete
                deletedsheetcount = deletedsheetcount + 1
            Else
                strMessage = strMessage & ws.Name & vbLf
            End If
        Next
        Application.DisplayAlerts = True
    MsgBox deletedsheetcount & " empty sheets deleted" & vbLf & _
                "Remaining sheets: " & vbLf & strMessage
    End Sub
    K :-)

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Ahh, you beat me to it Malc - and checking for chart sheets is, of course, a very good idea which I didn't consider...
    Maybe checking for shapes and other objects needs to be added?
    K :-)

  5. #5
    VBAX Regular
    Joined
    Jun 2005
    Posts
    13
    Location


    Thanks guys - that was very quick and both do the job marvelously

    Ross

    P.S. can I just add the deletedsheets count line and Msg box stuff to the code that also considers charts?
    Last edited by rosspmm; 06-30-2005 at 04:31 AM. Reason: additional comment

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •