PC Review


Reply
Thread Tools Rate Thread

deleting sheet

 
 
Seeker
Guest
Posts: n/a
 
      31st May 2009
I have a workbook with 3 work sheets, first tab is for data input, second tab
is for data maintain, third tab is macro auto add sheet which extract data
from data maintain sheet for reporting purpose. I would like to delete the
third sheet when either users switch to other tab or closing workbook. I get
the “delete” code but don’t know the code when users switch tab or close book.

Application.DisplayAlerts = False
Sheets("Print").Delete
Application.DisplayAlerts = True

 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      31st May 2009
Try the Workbook close event..From VBE double click 'ThisWorkbook'

Private Sub Workbook_BeforeClose(Cancel As Boolean)

OR

get the Sheet DeActivate event of the 3rd sheet
Private Sub Worksheet_Deactivate()


If this post helps click Yes
---------------
Jacob Skaria


"Seeker" wrote:

> I have a workbook with 3 work sheets, first tab is for data input, second tab
> is for data maintain, third tab is macro auto add sheet which extract data
> from data maintain sheet for reporting purpose. I would like to delete the
> third sheet when either users switch to other tab or closing workbook. I get
> the “delete” code but don’t know the code when users switch tab or close book.
>
> Application.DisplayAlerts = False
> Sheets("Print").Delete
> Application.DisplayAlerts = True
>

 
Reply With Quote
 
Seeker
Guest
Posts: n/a
 
      31st May 2009
Hi Jacob,
I added the code in my first post to Private Sub Workbook_BeforeClose(Cancel
As Boolean), the sheet was deleted before book closed, that’s great.
I have problem in the Sheet DeActivate event. The event code is attached to
the sheet when I built it - say Sheet1(Print), when changing tab, it only
works once, thereafter, code crash.
1) As Print sheet is added by macro upon request only, the sheet number
changes from Sheet1(Print) to Sheet2(Print) and so on, so the code cannot be
kept and attached to the new added (Print).
2) If I close the work book while I am on the Print page, both
“Workbook_BeforeClose” and “Sheet DeActivate event” activate at the same time
caused one of the “delete” action cannot be performed.
Any solution to solve this please?
Rgds

"Jacob Skaria" wrote:

> Try the Workbook close event..From VBE double click 'ThisWorkbook'
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
>
> OR
>
> get the Sheet DeActivate event of the 3rd sheet
> Private Sub Worksheet_Deactivate()
>
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Seeker" wrote:
>
> > I have a workbook with 3 work sheets, first tab is for data input, second tab
> > is for data maintain, third tab is macro auto add sheet which extract data
> > from data maintain sheet for reporting purpose. I would like to delete the
> > third sheet when either users switch to other tab or closing workbook. I get
> > the “delete” code but don’t know the code when users switch tab or close book.
> >
> > Application.DisplayAlerts = False
> > Sheets("Print").Delete
> > Application.DisplayAlerts = True
> >

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      31st May 2009
1. Use the below procedure to delete the sheet which contains the work
"Print". Call this procedure from Workbook close. If sheet exists the sheet
will be deleted..

Call DeletePrintSheet.

Sub DeletePrintSheet()
For intTEmp = 1 To Sheets.Count
If InStr(1, Sheets(intTEmp).Name, "print", vbTextCompare) <> 0 Then
Application.DisplayAlerts = False
Sheets(intTEmp).Delete: Exit For
Application.DisplayAlerts = True
End If
Next
End Sub

2. When you delete the sheet any code attached to that sheet will also get
deleted. So placing the code at deactivate will work only once...Why dont you
just hide the worksheet upon deactivate and then without adding a worksheet
just set the visible property to True. If you have any formatting or data
which needs to be cleared; clear those using code and keep it hidden...

Sheets("Sheet4").visible = false

If this post helps click Yes
---------------
Jacob Skaria


"Seeker" wrote:

> Hi Jacob,
> I added the code in my first post to Private Sub Workbook_BeforeClose(Cancel
> As Boolean), the sheet was deleted before book closed, that’s great.
> I have problem in the Sheet DeActivate event. The event code is attached to
> the sheet when I built it - say Sheet1(Print), when changing tab, it only
> works once, thereafter, code crash.
> 1) As Print sheet is added by macro upon request only, the sheet number
> changes from Sheet1(Print) to Sheet2(Print) and so on, so the code cannot be
> kept and attached to the new added (Print).
> 2) If I close the work book while I am on the Print page, both
> “Workbook_BeforeClose” and “Sheet DeActivate event” activate at the same time
> caused one of the “delete” action cannot be performed.
> Any solution to solve this please?
> Rgds
>
> "Jacob Skaria" wrote:
>
> > Try the Workbook close event..From VBE double click 'ThisWorkbook'
> >
> > Private Sub Workbook_BeforeClose(Cancel As Boolean)
> >
> > OR
> >
> > get the Sheet DeActivate event of the 3rd sheet
> > Private Sub Worksheet_Deactivate()
> >
> >
> > If this post helps click Yes
> > ---------------
> > Jacob Skaria
> >
> >
> > "Seeker" wrote:
> >
> > > I have a workbook with 3 work sheets, first tab is for data input, second tab
> > > is for data maintain, third tab is macro auto add sheet which extract data
> > > from data maintain sheet for reporting purpose. I would like to delete the
> > > third sheet when either users switch to other tab or closing workbook. I get
> > > the “delete” code but don’t know the code when users switch tab or close book.
> > >
> > > Application.DisplayAlerts = False
> > > Sheets("Print").Delete
> > > Application.DisplayAlerts = True
> > >

 
Reply With Quote
 
Seeker
Guest
Posts: n/a
 
      7th Jun 2009
Tks Jacob.

"Jacob Skaria" wrote:

> 1. Use the below procedure to delete the sheet which contains the work
> "Print". Call this procedure from Workbook close. If sheet exists the sheet
> will be deleted..
>
> Call DeletePrintSheet.
>
> Sub DeletePrintSheet()
> For intTEmp = 1 To Sheets.Count
> If InStr(1, Sheets(intTEmp).Name, "print", vbTextCompare) <> 0 Then
> Application.DisplayAlerts = False
> Sheets(intTEmp).Delete: Exit For
> Application.DisplayAlerts = True
> End If
> Next
> End Sub
>
> 2. When you delete the sheet any code attached to that sheet will also get
> deleted. So placing the code at deactivate will work only once...Why dont you
> just hide the worksheet upon deactivate and then without adding a worksheet
> just set the visible property to True. If you have any formatting or data
> which needs to be cleared; clear those using code and keep it hidden...
>
> Sheets("Sheet4").visible = false
>
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Seeker" wrote:
>
> > Hi Jacob,
> > I added the code in my first post to Private Sub Workbook_BeforeClose(Cancel
> > As Boolean), the sheet was deleted before book closed, that’s great.
> > I have problem in the Sheet DeActivate event. The event code is attached to
> > the sheet when I built it - say Sheet1(Print), when changing tab, it only
> > works once, thereafter, code crash.
> > 1) As Print sheet is added by macro upon request only, the sheet number
> > changes from Sheet1(Print) to Sheet2(Print) and so on, so the code cannot be
> > kept and attached to the new added (Print).
> > 2) If I close the work book while I am on the Print page, both
> > “Workbook_BeforeClose” and “Sheet DeActivate event” activate at the same time
> > caused one of the “delete” action cannot be performed.
> > Any solution to solve this please?
> > Rgds
> >
> > "Jacob Skaria" wrote:
> >
> > > Try the Workbook close event..From VBE double click 'ThisWorkbook'
> > >
> > > Private Sub Workbook_BeforeClose(Cancel As Boolean)
> > >
> > > OR
> > >
> > > get the Sheet DeActivate event of the 3rd sheet
> > > Private Sub Worksheet_Deactivate()
> > >
> > >
> > > If this post helps click Yes
> > > ---------------
> > > Jacob Skaria
> > >
> > >
> > > "Seeker" wrote:
> > >
> > > > I have a workbook with 3 work sheets, first tab is for data input, second tab
> > > > is for data maintain, third tab is macro auto add sheet which extract data
> > > > from data maintain sheet for reporting purpose. I would like to delete the
> > > > third sheet when either users switch to other tab or closing workbook. I get
> > > > the “delete” code but don’t know the code when users switch tab or close book.
> > > >
> > > > Application.DisplayAlerts = False
> > > > Sheets("Print").Delete
> > > > Application.DisplayAlerts = True
> > > >

 
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 a sheet with the same name LabrGuy Bob R Microsoft Excel Programming 9 5th Oct 2007 11:42 PM
deleting rows from one sheet based on 2nd sheet =?Utf-8?B?bWFnb2xkMjAwNUBob3RtYWlsLmNvbQ==?= Microsoft Excel Discussion 1 6th Sep 2007 07:38 AM
Deleting sheet Brandi Microsoft Excel Programming 2 7th Oct 2004 09:31 PM
Deleting Sheet with VBA Bre-x Microsoft Excel Discussion 1 2nd Sep 2003 08:08 PM
Deleting a Sheet Abdul Salam Microsoft Excel Programming 1 29th Jul 2003 12:43 AM


Features
 

Advertising
 

Newsgroups
 


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