PC Review


Reply
Thread Tools Rate Thread

Delet Sheets with out Automation Error

 
 
rogge
Guest
Posts: n/a
 
      13th Jan 2009
here is my awesome code:

Option Private Module
Option Compare Binary
Option Explicit
Option Base 0

Public Const strDeleteSheet = "delSht"

Private Sub deleteSheets()

Dim xlSheet As Excel.Worksheet

Application.DisplayAlerts = False

For Each xlSheet In Application.ActiveWorkbook.Worksheets
If (xlSheet.CodeName Like strDeleteSheet & "*") Then
wkbActive.Worksheets(xlSheet.Name).Delete
End If
Next xlSheet

Application.DisplayAlerts = True

Set xlSheet = Nothing

End Sub

A not trappable error occurs: xlSheet.Name = <Automation error>. The code
does not stop processing. I know this is due to changing the items in
ActiveWorkbook.Worksheets.

This error resets globally declared variables to empty/false/etc...

Any suggestions to delete sheets without resetting global variables?

Thank you.
rogge
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      13th Jan 2009
First, instead of:
wkbActive.Worksheets(xlSheet.Name).Delete

You may want to use:
Application.ActiveWorkbook.Worksheets(xlSheet.Name).Delete

Actually, I'd use:
xlSheet.Delete

In the code you posted, there's nothing that declares or sets wkbActive.

rogge wrote:
>
> here is my awesome code:
>
> Option Private Module
> Option Compare Binary
> Option Explicit
> Option Base 0
>
> Public Const strDeleteSheet = "delSht"
>
> Private Sub deleteSheets()
>
> Dim xlSheet As Excel.Worksheet
>
> Application.DisplayAlerts = False
>
> For Each xlSheet In Application.ActiveWorkbook.Worksheets
> If (xlSheet.CodeName Like strDeleteSheet & "*") Then
> wkbActive.Worksheets(xlSheet.Name).Delete
> End If
> Next xlSheet
>
> Application.DisplayAlerts = True
>
> Set xlSheet = Nothing
>
> End Sub
>
> A not trappable error occurs: xlSheet.Name = <Automation error>. The code
> does not stop processing. I know this is due to changing the items in
> ActiveWorkbook.Worksheets.
>
> This error resets globally declared variables to empty/false/etc...
>
> Any suggestions to delete sheets without resetting global variables?
>
> Thank you.
> rogge


--

Dave Peterson
 
Reply With Quote
 
rogge
Guest
Posts: n/a
 
      13th Jan 2009

wkbActive is declared elsewhere...
I do like xlSheet.Delete...

but i had one of our excel experts look at the code, etc.. the public
variables are being reset by some other code... and the "watch" is not
finding when the value changes.

"Dave Peterson" wrote:

> First, instead of:
> wkbActive.Worksheets(xlSheet.Name).Delete
>
> You may want to use:
> Application.ActiveWorkbook.Worksheets(xlSheet.Name).Delete
>
> Actually, I'd use:
> xlSheet.Delete
>
> In the code you posted, there's nothing that declares or sets wkbActive.
>
> rogge wrote:
> >
> > here is my awesome code:
> >
> > Option Private Module
> > Option Compare Binary
> > Option Explicit
> > Option Base 0
> >
> > Public Const strDeleteSheet = "delSht"
> >
> > Private Sub deleteSheets()
> >
> > Dim xlSheet As Excel.Worksheet
> >
> > Application.DisplayAlerts = False
> >
> > For Each xlSheet In Application.ActiveWorkbook.Worksheets
> > If (xlSheet.CodeName Like strDeleteSheet & "*") Then
> > wkbActive.Worksheets(xlSheet.Name).Delete
> > End If
> > Next xlSheet
> >
> > Application.DisplayAlerts = True
> >
> > Set xlSheet = Nothing
> >
> > End Sub
> >
> > A not trappable error occurs: xlSheet.Name = <Automation error>. The code
> > does not stop processing. I know this is due to changing the items in
> > ActiveWorkbook.Worksheets.
> >
> > This error resets globally declared variables to empty/false/etc...
> >
> > Any suggestions to delete sheets without resetting global variables?
> >
> > Thank you.
> > rogge

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th Jan 2009
What kind of code are you using that resets that variable?

Maybe it's better to fix that.

======
But for this situation, I would be surprised (but it's possible), that you
really meant the activeworkbook:

> > > For Each xlSheet In Application.ActiveWorkbook.Worksheets
> > > If (xlSheet.CodeName Like strDeleteSheet & "*") Then
> > > wkbActive.Worksheets(xlSheet.Name).Delete
> > > End If
> > > Next xlSheet


It would be pretty weird (but not unheard of) to loop through one workbook to
delete stuff in another.

rogge wrote:
>
> wkbActive is declared elsewhere...
> I do like xlSheet.Delete...
>
> but i had one of our excel experts look at the code, etc.. the public
> variables are being reset by some other code... and the "watch" is not
> finding when the value changes.
>
> "Dave Peterson" wrote:
>
> > First, instead of:
> > wkbActive.Worksheets(xlSheet.Name).Delete
> >
> > You may want to use:
> > Application.ActiveWorkbook.Worksheets(xlSheet.Name).Delete
> >
> > Actually, I'd use:
> > xlSheet.Delete
> >
> > In the code you posted, there's nothing that declares or sets wkbActive.
> >
> > rogge wrote:
> > >
> > > here is my awesome code:
> > >
> > > Option Private Module
> > > Option Compare Binary
> > > Option Explicit
> > > Option Base 0
> > >
> > > Public Const strDeleteSheet = "delSht"
> > >
> > > Private Sub deleteSheets()
> > >
> > > Dim xlSheet As Excel.Worksheet
> > >
> > > Application.DisplayAlerts = False
> > >
> > > For Each xlSheet In Application.ActiveWorkbook.Worksheets
> > > If (xlSheet.CodeName Like strDeleteSheet & "*") Then
> > > wkbActive.Worksheets(xlSheet.Name).Delete
> > > End If
> > > Next xlSheet
> > >
> > > Application.DisplayAlerts = True
> > >
> > > Set xlSheet = Nothing
> > >
> > > End Sub
> > >
> > > A not trappable error occurs: xlSheet.Name = <Automation error>. The code
> > > does not stop processing. I know this is due to changing the items in
> > > ActiveWorkbook.Worksheets.
> > >
> > > This error resets globally declared variables to empty/false/etc...
> > >
> > > Any suggestions to delete sheets without resetting global variables?
> > >
> > > Thank you.
> > > rogge

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
rogge
Guest
Posts: n/a
 
      14th Jan 2009
All of the code is affecting items in the activeworkbook. I don't use code
in this workbook to change objects in another workbook.

Only callbacks (procedures executed from the ribbon) set this boolean
variable.

I am going to ask this resetting question in a separate thread because i
have found that ending the code execution resets the boolean.... please
search for "rogge"

thanks for your help.

"Dave Peterson" wrote:

> What kind of code are you using that resets that variable?
>
> Maybe it's better to fix that.
>
> ======
> But for this situation, I would be surprised (but it's possible), that you
> really meant the activeworkbook:
>


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      14th Jan 2009
I don't use the End command (not End sub and not end if ...) in code. It ends
the running macro, but doesn't do it gracefully (as you've seen).

rogge wrote:
>
> All of the code is affecting items in the activeworkbook. I don't use code
> in this workbook to change objects in another workbook.
>
> Only callbacks (procedures executed from the ribbon) set this boolean
> variable.
>
> I am going to ask this resetting question in a separate thread because i
> have found that ending the code execution resets the boolean.... please
> search for "rogge"
>
> thanks for your help.
>
> "Dave Peterson" wrote:
>
> > What kind of code are you using that resets that variable?
> >
> > Maybe it's better to fix that.
> >
> > ======
> > But for this situation, I would be surprised (but it's possible), that you
> > really meant the activeworkbook:
> >


--

Dave Peterson
 
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
RE: Excel Automation - Delete sheets using access VBA ryguy7272 Microsoft Access VBA Modules 0 18th Nov 2009 05:30 AM
Delet certain row in a sheets in workbook Arne Hegefors Microsoft Excel Programming 1 5th Dec 2007 08:52 AM
Protect sheets via VB6 automation Maurizio BELLANTONE Microsoft Excel Programming 2 28th Jun 2006 09:25 AM
When I try to delet old message I get an error - =?Utf-8?B?bGVl?= Microsoft Outlook Discussion 0 16th Oct 2005 02:23 AM
Automation for attendance sheets =?Utf-8?B?bmVpbA==?= Microsoft Excel Misc 1 6th Oct 2004 12:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:11 AM.