• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Delete sheets with macro

KreshBell

New Member
Hi all,
I have an excel template that is filled with data from CAD system. The template has always the same number of sheets, but sometimes CAD fills all sheets, sometimes only some. If it does not fill the sheet, only the header remains in that sheet. I call all sheets that only have a header "empty".
excel sample.jpg

After CAD extracts the data into Excel, I would like to delete all sheets that only have a header. Is it possible to make a macro for that?
 
I'm assuming that the sheets with the data all have cell A2 populated, and also that when you start the macro the active workbook is this workbook with the data.
Try this macro of mine:
Code:
Option Explicit
Sub DeleteSheetsWithNoData()
    Dim sht, cnt As Integer
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each sht In Worksheets                    'loop all sheets in workbook
        If sht.Range("A2") = "" Then
            sht.Delete                            'if cell A2 is empty delete sheet
            cnt = cnt + 1
        End If
    Next sht
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    If cnt Then
        MsgBox "Done! deleted " & cnt & " sheets"
    Else
        MsgBox "No sheets to delete!"
    End If
End Sub
 
Hi ! @rollis13 : for the fun a demonstration deleting worksheets at once, to better paste to ThisWorkbook module :​
Code:
Sub Demo1()
       Const C = "¤"
         Dim Ws As Worksheet, S$
    For Each Ws In Worksheets
        If IsEmpty(Ws.[A2]) Then S = S & String(-(S > ""), C) & Ws.Name
    Next
    If S > "" Then
        Application.DisplayAlerts = False
        Worksheets(Split(S, C)).Delete
        Application.DisplayAlerts = True
    End If
End Sub
 
Back
Top