Consulting

Results 1 to 9 of 9

Thread: Delete Empty Sheets

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    29
    Location

    Delete Empty Sheets

    Hi All, I'd like the macro to delete all the blank sheets of the workbook, or each sheet in which cell "A1" is empty (whichever way is easier to code). I tried the following code, but it is not working correctly. Any help would be appreciated. Thanks!
    [vba]
    With ActiveWorkbook
    For Each Worksheet In ThisWorkbook.Worksheets
    If .Cells(1, "A").Value = "" Then
    Application.DisplayAlerts = False
    ActiveSheet.Delete
    Application.DisplayAlerts = True
    End If
    Next Worksheet
    End With
    [/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    With ActiveWorkbook

    For Each sh In ThisWorkbook.Worksheets

    If sh.Cells(1, "A").Value = "" Then

    Application.DisplayAlerts = False
    sh.Delete
    Application.DisplayAlerts = True
    End If
    Next sh
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Feb 2009
    Posts
    29
    Location
    Thanks for your quick reply Bob. However, when I run the code now I am getting an error stating that the workbook must contain at least one sheet -- but not all sheets are blank so not all of them should be deleted. Any idea whats going wrong? Thanks.

  4. #4
    VBAX Regular
    Joined
    Feb 2009
    Posts
    29
    Location

    Solved: Delete Empty Sheets

    Nevermind the previous error. I found another solution using the count function rather than checking the value of cell A1. This works without error:
    [vba]For Each sh In Sheets
    If Application.WorksheetFunction.CountA(sh.Cells) = 0 Then
    Application.DisplayAlerts = False
    sh.Delete
    Application.DisplayAlerts = True
    End If
    Next sh[/vba]

    Thanks again for your help.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That gives the same problem surely? If all sheets could be empty then try this

    [vba]

    With ActiveWorkbook

    For Each sh In ThisWorkbook.Worksheets

    If sh.Cells(1, "A").Value = "" And ThisWorkbook.Worksheets .Count > 1 Then

    Application.DisplayAlerts = False
    sh.Delete
    Application.DisplayAlerts = True
    End If
    Next sh
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Feb 2009
    Posts
    29
    Location
    Surprisingly, the code I posted with the count function does not produce an error...I'm not sure why. In my workbook, its impossible for all the sheets to be empty, so I'm not sure why I was getting that error.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Probably because you originally stated ... delete all the blank sheets of the workbook, or each sheet in which cell "A1" is empty (whichever way is easier ... I used thge cell A1, so you must have sheets that are NOT empty, but A1 is.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Feb 2009
    Posts
    29
    Location
    That's not the case either...all the sheets that are not empty contain a date value in cell A1. Either way, we found a solution that will work...I appreciate your help. Thanks!

  9. #9
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    But it is clear that the macro won't delete the sheet when A1 is filled, that's weird.

Posting Permissions

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