PC Review


Reply
Thread Tools Rate Thread

Delete Sheet executing macro

 
 
DocBrown
Guest
Posts: n/a
 
      15th Aug 2009
I have a sheet that has a command button. One of the tasks I want the macro
do to is delete the sheet that the command button is on. What is the proper
way to do that?

What do you think of this:

On Sheet3 code:

Private Sub cmdMigrateWB_Click()
MigrateWorkbook
end sub

In Module1:

Sub MigrateWorkbook()

On Error GoTo ErrThisSub

' Do a bunch of stuff

prevValue = Application.DisplayAlerts
Application.DisplayAlerts = False
On Error Resume Next

'delete the sheet the cmd button was on
Sheets(3).Delete
On Error GoTo ErrThisSub
Application.DisplayAlerts = prevValue

' do some more stuff

Exit Sub

ErrThisSub:
MsgBox "Error in routine."

End Sub

It seems to work, but it seems wrong to delete the code that the Module1
macro is trying to return to. The main effect I've seen is that I can't enter
debug after the delete has occured.

Suggestions?

Thanks,
John
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      15th Aug 2009
It may be deleting the wrong worksheet. Try deleting the worksheet by its
name instead of the number

Sheets("sheet3").Delete


"DocBrown" wrote:

> I have a sheet that has a command button. One of the tasks I want the macro
> do to is delete the sheet that the command button is on. What is the proper
> way to do that?
>
> What do you think of this:
>
> On Sheet3 code:
>
> Private Sub cmdMigrateWB_Click()
> MigrateWorkbook
> end sub
>
> In Module1:
>
> Sub MigrateWorkbook()
>
> On Error GoTo ErrThisSub
>
> ' Do a bunch of stuff
>
> prevValue = Application.DisplayAlerts
> Application.DisplayAlerts = False
> On Error Resume Next
>
> 'delete the sheet the cmd button was on
> Sheets(3).Delete
> On Error GoTo ErrThisSub
> Application.DisplayAlerts = prevValue
>
> ' do some more stuff
>
> Exit Sub
>
> ErrThisSub:
> MsgBox "Error in routine."
>
> End Sub
>
> It seems to work, but it seems wrong to delete the code that the Module1
> macro is trying to return to. The main effect I've seen is that I can't enter
> debug after the delete has occured.
>
> Suggestions?
>
> Thanks,
> John

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      15th Aug 2009
If your delete code is in the sheet code module of the deleted sheet, you
will not be able to return to the code because you will have deleted it.
Put the code in the project module (Module1) and it should then allow you to
return to the code after the delete event.



"DocBrown" <(E-Mail Removed)> wrote in message
news:344068E2-C487-420E-9C54-(E-Mail Removed)...
>I have a sheet that has a command button. One of the tasks I want the macro
> do to is delete the sheet that the command button is on. What is the
> proper
> way to do that?
>
> What do you think of this:
>
> On Sheet3 code:
>
> Private Sub cmdMigrateWB_Click()
> MigrateWorkbook
> end sub
>
> In Module1:
>
> Sub MigrateWorkbook()
>
> On Error GoTo ErrThisSub
>
> ' Do a bunch of stuff
>
> prevValue = Application.DisplayAlerts
> Application.DisplayAlerts = False
> On Error Resume Next
>
> 'delete the sheet the cmd button was on
> Sheets(3).Delete
> On Error GoTo ErrThisSub
> Application.DisplayAlerts = prevValue
>
> ' do some more stuff
>
> Exit Sub
>
> ErrThisSub:
> MsgBox "Error in routine."
>
> End Sub
>
> It seems to work, but it seems wrong to delete the code that the Module1
> macro is trying to return to. The main effect I've seen is that I can't
> enter
> debug after the delete has occured.
>
> Suggestions?
>
> Thanks,
> John



 
Reply With Quote
 
DocBrown
Guest
Posts: n/a
 
      24th Aug 2009
The Sub MigrateWorkbook that is doing all the work is in the Module1 code.
The sheet being deleted only has a call to the Sub in Module1:

Private Sub cmdMigrateWB_Click()
MigrateWorkbook
end sub


This issue is that I need to operate on the Click event of the command
button that is on the sheet that will be deleted. Is there another way to
intercept the click event of the command button on the worksheet?

"JLGWhiz" wrote:

> If your delete code is in the sheet code module of the deleted sheet, you
> will not be able to return to the code because you will have deleted it.
> Put the code in the project module (Module1) and it should then allow you to
> return to the code after the delete event.
>
>
>
> "DocBrown" <(E-Mail Removed)> wrote in message
> news:344068E2-C487-420E-9C54-(E-Mail Removed)...
> >I have a sheet that has a command button. One of the tasks I want the macro
> > do to is delete the sheet that the command button is on. What is the
> > proper
> > way to do that?
> >
> > What do you think of this:
> >
> > On Sheet3 code:
> >
> > Private Sub cmdMigrateWB_Click()
> > MigrateWorkbook
> > end sub
> >
> > In Module1:
> >
> > Sub MigrateWorkbook()
> >
> > On Error GoTo ErrThisSub
> >
> > ' Do a bunch of stuff
> >
> > prevValue = Application.DisplayAlerts
> > Application.DisplayAlerts = False
> > On Error Resume Next
> >
> > 'delete the sheet the cmd button was on
> > Sheets(3).Delete
> > On Error GoTo ErrThisSub
> > Application.DisplayAlerts = prevValue
> >
> > ' do some more stuff
> >
> > Exit Sub
> >
> > ErrThisSub:
> > MsgBox "Error in routine."
> >
> > End Sub
> >
> > It seems to work, but it seems wrong to delete the code that the Module1
> > macro is trying to return to. The main effect I've seen is that I can't
> > enter
> > debug after the delete has occured.
> >
> > Suggestions?
> >
> > Thanks,
> > John

>
>
>

 
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
Macro to copy a sheet and delete a macro button JoeP Microsoft Excel Programming 8 30th Sep 2009 06:14 PM
need to modify a macro to prevent it from executing if in wrong area of the sheet Tonso Microsoft Excel Misc 3 11th Feb 2007 08:00 PM
Macro for sheet delete, very easy comotoman Microsoft Excel Misc 1 11th Oct 2005 11:19 PM
Macro Delete Sheet Stephen Microsoft Excel Misc 4 17th Mar 2004 02:43 AM
macro to delete a sheet MoiraGunn Microsoft Excel Programming 2 3rd Sep 2003 06:11 AM


Features
 

Advertising
 

Newsgroups
 


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