Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Delete Sheet executing macro

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Delete Sheet executing macro

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Delete Sheet executing macro

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" wrote in message
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Delete Sheet executing macro

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" wrote in message
...
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use macro to delete worksheet without specific name Eg:Sheet 1 Delight Excel Programming 5 November 19th 08 01:01 AM
need to modify a macro to prevent it from executing if in wrong area of the sheet Tonso Excel Discussion (Misc queries) 3 February 11th 07 08:00 PM
Macro for sheet delete, very easy comotoman Excel Discussion (Misc queries) 1 October 11th 05 11:19 PM
recording macro to delete sheet Marcia[_2_] Excel Programming 5 May 12th 04 10:13 PM
macro to delete a sheet MoiraGunn Excel Programming 2 September 3rd 03 06:11 AM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"