Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 10th, 2005, 03:59 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default Delete all empty worksheets

I'm after code that can
Delete all empty worksheets.

I have this, but it doesnt work.

I get method 'delete' of object 'worksheet' failed.

Code:
Dim sh As Worksheet
    For Each sh In ThisWorkbook.Worksheets
      If Application.WorksheetFunction.CountA(sh.Cells) = 0 Then
        Application.DisplayAlerts = False
        sh.Delete
        Application.DisplayAlerts = True
      End If
    Next
TIA.

Picco

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old May 10th, 2005, 06:27 AM
Authorized User
 
Join Date: Aug 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You code seems to be OK. You do not mention if sheets are deleted.

You are probably trying to delete the one remaining sheet, which is not possible. A workbook must contain at least one sheet.

-----------------------
Regards BrianB
Most problems occur from starting at the wrong place.
Use a cup of coffee to make Windows run faster.
It is easy until you know how.
 
Old May 10th, 2005, 07:08 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Code:
Sub DeleteBlankSheets()
    Dim sh As Variant

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
On Error GoTo Exits:

    For Each sh In Sheets
        If Not IsChart(sh) Then
            If Application.WorksheetFunction.CountA(sh.Cells) = 0 Then sh.Delete
        End If
    Next sh

Exits:
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Public Function IsChart(sh) As Boolean
    Dim tmpChart As Chart
    On Error Resume Next
    Set tmpChart = Charts(sh.Name)
    IsChart = IIf(tmpChart Is Nothing, False, True)
End Function
that works

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Worksheets brawny4 Excel VBA 2 December 29th, 2007 04:57 AM
Worksheets (tabs) paul20091968 Excel VBA 1 December 12th, 2006 04:33 AM
delete all empty cells and move them to the left crmpicco Excel VBA 1 May 6th, 2005 05:47 AM
delete/ignore empty column crmpicco Excel VBA 1 May 4th, 2005 08:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.