PC Review


Reply
Thread Tools Rate Thread

Deleting a Worksheet??

 
 
Scott
Guest
Posts: n/a
 
      23rd Sep 2008
I have a macro that, depending on the data, will sometimes create a separate
sheet entitled "MC TABLE". I also have a second Macro within the same program
that will delete and clear the needed sections of the different worksheets so
that it can be ready for the next batch of data on the next day. The problem
is that the "MC TABLE" sheet needs to be deleted entirely but on the
occasions that it does not exist, I get an error message. How can I write the
code so that it tests to see if that sheet exists before attempting to select
or delete it??
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      23rd Sep 2008
Try this

You don't need to check, simply do this

Sub marine3()
Application.DisplayAlerts = False
On Error Resume Next
Sheets("MC TABLE").Delete
Application.DisplayAlerts = True
On Error GoTo 0
End Sub

But if uo want to

Sub marine()
Dim WS As Worksheet
On Error Resume Next
Set WS = Sheets("MC TABLE")
On Error GoTo 0
If Not WS Is Nothing Then
MsgBox "I exist"
Application.DisplayAlerts = False
WS.Delete
Application.DisplayAlerts = True
Else
MsgBox "I don't exist"
End If
End Sub


Mike

"Scott" wrote:

> I have a macro that, depending on the data, will sometimes create a separate
> sheet entitled "MC TABLE". I also have a second Macro within the same program
> that will delete and clear the needed sections of the different worksheets so
> that it can be ready for the next batch of data on the next day. The problem
> is that the "MC TABLE" sheet needs to be deleted entirely but on the
> occasions that it does not exist, I get an error message. How can I write the
> code so that it tests to see if that sheet exists before attempting to select
> or delete it??

 
Reply With Quote
 
Frederik
Guest
Posts: n/a
 
      23rd Sep 2008
Read the tips of John Walkenbach: very usefull!!!!!!

http://spreadsheetpage.com/index.php...vba_functions/

Success!!!

--
met vriendelijke groetjes

(E-Mail Removed)
"Scott" <(E-Mail Removed)> schreef in bericht
news:929BE3E9-CDEB-4BCE-AB03-(E-Mail Removed)...
>I have a macro that, depending on the data, will sometimes create a
>separate
> sheet entitled "MC TABLE". I also have a second Macro within the same
> program
> that will delete and clear the needed sections of the different worksheets
> so
> that it can be ready for the next batch of data on the next day. The
> problem
> is that the "MC TABLE" sheet needs to be deleted entirely but on the
> occasions that it does not exist, I get an error message. How can I write
> the
> code so that it tests to see if that sheet exists before attempting to
> select
> or delete it??



 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      23rd Sep 2008
Thank you to both. Both answers proved useful for this question and another
issue I was working on.

Scott

"Scott" wrote:

> I have a macro that, depending on the data, will sometimes create a separate
> sheet entitled "MC TABLE". I also have a second Macro within the same program
> that will delete and clear the needed sections of the different worksheets so
> that it can be ready for the next batch of data on the next day. The problem
> is that the "MC TABLE" sheet needs to be deleted entirely but on the
> occasions that it does not exist, I get an error message. How can I write the
> code so that it tests to see if that sheet exists before attempting to select
> or delete it??

 
Reply With Quote
 
Scott
Guest
Posts: n/a
 
      23rd Sep 2008
Hi Mike,

The short version worked great but was wondering what "On Error GoTo 0"
actually does?? The rest made sense to me such as "On Error Resume Next"
just means if an error occurs on a line then skip it and start with the next
line of code.

Thanks,

Scott


"Mike H" wrote:

> Try this
>
> You don't need to check, simply do this
>
> Sub marine3()
> Application.DisplayAlerts = False
> On Error Resume Next
> Sheets("MC TABLE").Delete
> Application.DisplayAlerts = True
> On Error GoTo 0
> End Sub
>
> But if uo want to
>
> Sub marine()
> Dim WS As Worksheet
> On Error Resume Next
> Set WS = Sheets("MC TABLE")
> On Error GoTo 0
> If Not WS Is Nothing Then
> MsgBox "I exist"
> Application.DisplayAlerts = False
> WS.Delete
> Application.DisplayAlerts = True
> Else
> MsgBox "I don't exist"
> End If
> End Sub
>
>
> Mike
>
> "Scott" wrote:
>
> > I have a macro that, depending on the data, will sometimes create a separate
> > sheet entitled "MC TABLE". I also have a second Macro within the same program
> > that will delete and clear the needed sections of the different worksheets so
> > that it can be ready for the next batch of data on the next day. The problem
> > is that the "MC TABLE" sheet needs to be deleted entirely but on the
> > occasions that it does not exist, I get an error message. How can I write the
> > code so that it tests to see if that sheet exists before attempting to select
> > or delete it??

 
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
Deleting column in a worksheet that is not active worksheet kansaskannan@gmail.com Microsoft Excel Programming 3 7th Oct 2007 03:33 PM
Deleting a worksheet but retaining values from the worksheet. helpdesk@t7.net.au Microsoft Excel Misc 1 13th Sep 2006 03:00 PM
Deleting a worksheet but retaining values from the worksheet. helpdesk@t7.net.au Microsoft Excel Misc 1 13th Sep 2006 02:48 PM
deleting values in a worksheet without deleting the formulas =?Utf-8?B?cGF0dGk=?= Microsoft Excel Worksheet Functions 1 28th Oct 2005 09:49 PM
Deleting Worksheet shimeel Microsoft Excel Misc 1 28th Oct 2004 07:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:08 PM.