+ Reply to Thread
Results 1 to 7 of 7

Delete Worksheets That Contain..

Hybrid View

  1. #1
    Registered User
    Join Date
    08-21-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    41

    Delete Worksheets That Contain..

    I have a workbook that contains over 100 sheets, all built from templates. Some of them contain data and some do not. i'd like to delete all of the ones that do not contain data. I found and modified this code from another post.
    Sub delete()
    Application.DisplayAlerts = False
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
    If IsEmpty(Range("C9")) Then ws.delete
    Next ws
    Application.DisplayAlerts = True
    
    End Sub
    This code does not work but it at least shows what i'm trying to do.
    Last edited by sjennings007; 02-26-2014 at 12:45 PM.

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: Delete Worksheets That Contain..

    what part does not work?

  3. #3
    Registered User
    Join Date
    08-21-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    41

    Re: Delete Worksheets That Contain..

    shg,

    I'm not familiar with what your if statement is saying.

    PS. nothing happens when i try that. Note that all of the worksheets have stuff in them (since they were created with a template) but only some have user entered data.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Delete Worksheets That Contain..

        Dim wks As Worksheet
    
        
        Application.DisplayAlerts = False
        For Each wks In Worksheets
            If WorksheetFunction.CountA(wks.UsedRange) = 0 Then wks.Delete
        Next wks
        Application.DisplayAlerts = True
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    08-21-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    41

    Re: Delete Worksheets That Contain..

    When I run the code, it deletes all spreadsheet where this is true and then stops the first time it is false. If i click on the next spreadsheet where this is true, it then deletes the prior spreadsheet (where its false), and then continues deleting true worksheets until it hits the next false one.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Delete Worksheets That Contain..

    It counts cells containing anything; if there are none, it deletes the sheet.

    In your code,

    IsEmpty(Range("C9")) ...
    Range refers to the active worksheet, not the worksheet of the loop variable, which would be

    IsEmpty(ws.Range("C9")) ...

  7. #7
    Registered User
    Join Date
    08-21-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    41

    Re: Delete Worksheets That Contain..

    Ok, cool. thanks shg.

    So in this case where every sheet does contain stuff, it won't work?

    PS adding ws.range.... worked. problem solved thanks.
    Last edited by sjennings007; 02-26-2014 at 12:43 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 11-30-2013, 06:37 AM
  2. Delete all worksheets except the first one
    By Nithya1987 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-05-2011, 03:02 AM
  3. Do while loop to delete worksheets if worksheets are not in the specified list
    By kchm_2000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-26-2011, 03:02 PM
  4. Do While Loop to Delete Worksheets
    By kchm_2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2011, 01:34 PM
  5. I can't delete my worksheets
    By chuckmcc in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-17-2006, 06:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1