PC Review


Reply
Thread Tools Rate Thread

Delete Sheets Without Contents

 
 
Sal
Guest
Posts: n/a
 
      6th Aug 2009
I would like to get help to improve the macro below so that if Sheet2 or
Sheet3 or both, have no contents in their worksheets they will get deleted.
On the other hand if Sheet2 or Sheet3, or both have contents they will stay.
Here is the code I have right now. Thank you for your help.

Sub Deletesheetswithoutcontents()
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
Sheets("Sheet3").Delete
Application.DisplayAlerts = True

End Sub

 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      7th Aug 2009
(Untested) This should do what you want...

Sub DeleteEmptySheets2or3()
Application.DisplayAlerts = False
If Sheets("Sheet2").Cells.Find("*") Is Nothing Then Sheets("Sheet2").Delete
If Sheets("Sheet3").Cells.Find("*") Is Nothing Then Sheets("Sheet3").Delete
Application.DisplayAlerts = True
End Sub

--
Rick (MVP - Excel)


"Sal" <(E-Mail Removed)> wrote in message news:8900C852-6FBF-490F-B940-(E-Mail Removed)...
>I would like to get help to improve the macro below so that if Sheet2 or
> Sheet3 or both, have no contents in their worksheets they will get deleted.
> On the other hand if Sheet2 or Sheet3, or both have contents they will stay.
> Here is the code I have right now. Thank you for your help.
>
> Sub Deletesheetswithoutcontents()
> Application.DisplayAlerts = False
> Sheets("Sheet2").Delete
> Sheets("Sheet3").Delete
> Application.DisplayAlerts = True
>
> End Sub
>

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      7th Aug 2009
Helo Sal,

I like Rick's method of testing for data also but I would also test for the
existance of the worksheets otherwise if the code is run again after the
sheets are deleted then it will error out so have posted another option
including a different method of testing for existance of data.

The Else with msgbox if sheet contains data is optional.

Note that the space and underscore at the end of a line is a line break in
an otherwise single line of code.

Sub DeleteSheetsWithoutContents()

Dim ws As Worksheet

Application.DisplayAlerts = False

On Error Resume Next
Set ws = Sheets("Sheet2")
On Error GoTo 0

If Not ws Is Nothing Then 'Sheet exists
If WorksheetFunction. _
CountA(ws.UsedRange) = 0 Then
ws.Delete
Else
MsgBox ws.Name & " contains data"
End If
End If

Set ws = Nothing

On Error Resume Next
Set ws = Sheets("Sheet3")
On Error GoTo 0

If Not ws Is Nothing Then 'Sheet exists
If WorksheetFunction. _
CountA(ws.UsedRange) = 0 Then
ws.Delete
Else
MsgBox ws.Name & " contains data"
End If
End If

Application.DisplayAlerts = True

End Sub


--
Regards,

OssieMac


 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      7th Aug 2009
Sub testshts()
Application.DisplayAlerts = False
On Error Resume Next
For i = Worksheets.Count To 2 Step -1
If Sheets(i).Cells.Find(What:="*", After:=[A1], _
SearchDirection:=xlPrevious).Address = " " Then
'MsgBox Sheets(i).Name
Sheets(i).Delete
End If
Next i
Application.DisplayAlerts = True
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Sal" <(E-Mail Removed)> wrote in message
news:8900C852-6FBF-490F-B940-(E-Mail Removed)...
>I would like to get help to improve the macro below so that if Sheet2 or
> Sheet3 or both, have no contents in their worksheets they will get
> deleted.
> On the other hand if Sheet2 or Sheet3, or both have contents they will
> stay.
> Here is the code I have right now. Thank you for your help.
>
> Sub Deletesheetswithoutcontents()
> Application.DisplayAlerts = False
> Sheets("Sheet2").Delete
> Sheets("Sheet3").Delete
> Application.DisplayAlerts = True
>
> End Sub
>


 
Reply With Quote
 
Sal
Guest
Posts: n/a
 
      7th Aug 2009
Thank you Sir. This is very helpful.

"Rick Rothstein" wrote:

> (Untested) This should do what you want...
>
> Sub DeleteEmptySheets2or3()
> Application.DisplayAlerts = False
> If Sheets("Sheet2").Cells.Find("*") Is Nothing Then Sheets("Sheet2").Delete
> If Sheets("Sheet3").Cells.Find("*") Is Nothing Then Sheets("Sheet3").Delete
> Application.DisplayAlerts = True
> End Sub
>
> --
> Rick (MVP - Excel)
>
>
> "Sal" <(E-Mail Removed)> wrote in message news:8900C852-6FBF-490F-B940-(E-Mail Removed)...
> >I would like to get help to improve the macro below so that if Sheet2 or
> > Sheet3 or both, have no contents in their worksheets they will get deleted.
> > On the other hand if Sheet2 or Sheet3, or both have contents they will stay.
> > Here is the code I have right now. Thank you for your help.
> >
> > Sub Deletesheetswithoutcontents()
> > Application.DisplayAlerts = False
> > Sheets("Sheet2").Delete
> > Sheets("Sheet3").Delete
> > Application.DisplayAlerts = True
> >
> > End Sub
> >

>

 
Reply With Quote
 
Sal
Guest
Posts: n/a
 
      7th Aug 2009
Cool code. It works well. I am grateful for your help. Thank you.

"Don Guillett" wrote:

> Sub testshts()
> Application.DisplayAlerts = False
> On Error Resume Next
> For i = Worksheets.Count To 2 Step -1
> If Sheets(i).Cells.Find(What:="*", After:=[A1], _
> SearchDirection:=xlPrevious).Address = " " Then
> 'MsgBox Sheets(i).Name
> Sheets(i).Delete
> End If
> Next i
> Application.DisplayAlerts = True
> End Sub
>
> --
> Don Guillett
> Microsoft MVP Excel
> SalesAid Software
> (E-Mail Removed)
> "Sal" <(E-Mail Removed)> wrote in message
> news:8900C852-6FBF-490F-B940-(E-Mail Removed)...
> >I would like to get help to improve the macro below so that if Sheet2 or
> > Sheet3 or both, have no contents in their worksheets they will get
> > deleted.
> > On the other hand if Sheet2 or Sheet3, or both have contents they will
> > stay.
> > Here is the code I have right now. Thank you for your help.
> >
> > Sub Deletesheetswithoutcontents()
> > Application.DisplayAlerts = False
> > Sheets("Sheet2").Delete
> > Sheets("Sheet3").Delete
> > Application.DisplayAlerts = True
> >
> > End Sub
> >

>
>

 
Reply With Quote
 
Sal
Guest
Posts: n/a
 
      7th Aug 2009
You make some interesting points that are good to think about. I appreciate
your assistance. Thank you friend. The macro works very nicely.

"OssieMac" wrote:

> Helo Sal,
>
> I like Rick's method of testing for data also but I would also test for the
> existance of the worksheets otherwise if the code is run again after the
> sheets are deleted then it will error out so have posted another option
> including a different method of testing for existance of data.
>
> The Else with msgbox if sheet contains data is optional.
>
> Note that the space and underscore at the end of a line is a line break in
> an otherwise single line of code.
>
> Sub DeleteSheetsWithoutContents()
>
> Dim ws As Worksheet
>
> Application.DisplayAlerts = False
>
> On Error Resume Next
> Set ws = Sheets("Sheet2")
> On Error GoTo 0
>
> If Not ws Is Nothing Then 'Sheet exists
> If WorksheetFunction. _
> CountA(ws.UsedRange) = 0 Then
> ws.Delete
> Else
> MsgBox ws.Name & " contains data"
> End If
> End If
>
> Set ws = Nothing
>
> On Error Resume Next
> Set ws = Sheets("Sheet3")
> On Error GoTo 0
>
> If Not ws Is Nothing Then 'Sheet exists
> If WorksheetFunction. _
> CountA(ws.UsedRange) = 0 Then
> ws.Delete
> Else
> MsgBox ws.Name & " contains data"
> End If
> End If
>
> Application.DisplayAlerts = True
>
> End Sub
>
>
> --
> Regards,
>
> OssieMac
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could someone please help me w/ matching the contents of each two sheets? bxc2739 Microsoft Excel Misc 4 26th Apr 2006 11:04 PM
Re: Macro to delete sheets and saves remaining file does not properly delete module gazornenplat Microsoft Excel Programming 0 22nd Jun 2005 01:12 AM
Macro to delete sheets and saves remaining file does not properly delete module pherrero Microsoft Excel Programming 7 21st Jun 2005 05:16 PM
Add the contents of two sheets together mwiggins367 Microsoft Excel Misc 3 27th May 2004 04:24 PM
File Properties->Contents (tab)->Document Contents->fonts used (delete a font) Marina Microsoft Powerpoint 1 22nd Oct 2003 05:56 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:16 PM.