PC Review


Reply
Thread Tools Rate Thread

Code execution error

 
 
Tom
Guest
Posts: n/a
 
      4th Feb 2008
Hi all,
I have a workbook that has been running fine for a very long time. Last week
I noticed that all of a sudden I am getting an error that states "Code
execution has been interrupted". The macro still completes the operation, but
the error box comes up.
I have not altered the code in any way for quite some time, but this error
mysteriously started happening last week seemingly out of the blue. Any clues
as to what it could be and fixes for this?

Public Sub Print_Visible_Worksheets()
Application.ScreenUpdating = False
Dim sh As Worksheet
Dim arr() As String
Dim N As Long
Const sStr As String = "Main"

For Each sh In ThisWorkbook.Worksheets
With sh
If .Visible = xlSheetVisible Then
If StrComp(.Name, sStr, vbTextCompare) Then
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = .Name
End If
End If
End With
Next sh

With ThisWorkbook
.Worksheets(arr).PrintOut
.Worksheets(1).Select <-----ERROR IS ON THIS LINE
End With
Application.ScreenUpdating = True
End Sub
 
Reply With Quote
 
 
 
 
RichardSchollar
Guest
Posts: n/a
 
      4th Feb 2008
Hi Tom

Try placing the following line at the start of your code (after your
sub name):

Application.EnableCancelKey = xlDisabled

and see if the error goes away.

Richard




On 4 Feb, 11:24, Tom <T...@discussions.microsoft.com> wrote:
> Hi all,
> I have a workbook that has been running fine for a very long time. Last week
> I noticed that all of a sudden I am getting an error that states "Code
> execution has been interrupted". The macro still completes the operation, but
> the error box comes up.
> I have not altered the code in any way for quite some time, but this error
> mysteriously started happening last week seemingly out of the blue. Any clues
> as to what it could be and fixes for this?
>
> Public Sub Print_Visible_Worksheets()
> * * Application.ScreenUpdating = False
> * * Dim sh As Worksheet
> * * Dim arr() As String
> * * Dim N As Long
> * * Const sStr As String = "Main"
>
> * * For Each sh In ThisWorkbook.Worksheets
> * * * * With sh
> * * * * * * If .Visible = xlSheetVisible Then
> * * * * * * * * If StrComp(.Name, sStr, vbTextCompare) Then
> * * * * * * * * * * N = N + 1
> * * * * * * * * * * ReDim Preserve arr(1 To N)
> * * * * * * * * * * arr(N) = .Name
> * * * * * * * * End If
> * * * * * * End If
> * * * * End With
> * * Next sh
>
> * * With ThisWorkbook
> * * * * .Worksheets(arr).PrintOut
> * * * * .Worksheets(1).Select *<-----ERROR IS ON THIS LINE
> * * End With
> * * Application.ScreenUpdating = True
> End Sub


 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      4th Feb 2008
Hi Richard,
That seems to fix the problem. Now, in order that I can understand
this....WHY would this fix the problem...and why, all of a sudden, would it
start causing a problem to begin with?
I have had this macro written for almost a year without this error...so why
did it start all of a sudden?

"RichardSchollar" wrote:

> Hi Tom
>
> Try placing the following line at the start of your code (after your
> sub name):
>
> Application.EnableCancelKey = xlDisabled
>
> and see if the error goes away.
>
> Richard
>
>
>
>
> On 4 Feb, 11:24, Tom <T...@discussions.microsoft.com> wrote:
> > Hi all,
> > I have a workbook that has been running fine for a very long time. Last week
> > I noticed that all of a sudden I am getting an error that states "Code
> > execution has been interrupted". The macro still completes the operation, but
> > the error box comes up.
> > I have not altered the code in any way for quite some time, but this error
> > mysteriously started happening last week seemingly out of the blue. Any clues
> > as to what it could be and fixes for this?
> >
> > Public Sub Print_Visible_Worksheets()
> > Application.ScreenUpdating = False
> > Dim sh As Worksheet
> > Dim arr() As String
> > Dim N As Long
> > Const sStr As String = "Main"
> >
> > For Each sh In ThisWorkbook.Worksheets
> > With sh
> > If .Visible = xlSheetVisible Then
> > If StrComp(.Name, sStr, vbTextCompare) Then
> > N = N + 1
> > ReDim Preserve arr(1 To N)
> > arr(N) = .Name
> > End If
> > End If
> > End With
> > Next sh
> >
> > With ThisWorkbook
> > .Worksheets(arr).PrintOut
> > .Worksheets(1).Select <-----ERROR IS ON THIS LINE
> > End With
> > Application.ScreenUpdating = True
> > End Sub

>
>

 
Reply With Quote
 
RichardSchollar
Guest
Posts: n/a
 
      4th Feb 2008
Tom

I'm afraid I can't answer that - there's nothing wrong with your
code. It seems to be an Excel bug, and disabling the cancel key seems
to be a workaround for it.

Sorry I can't be any more helpful than that!

Richard


On 4 Feb, 11:55, Tom <T...@discussions.microsoft.com> wrote:
> Hi Richard,
> That seems to fix the problem. Now, in order that I can understand
> this....WHY would this fix the problem...and why, all of a sudden, would it
> start causing a problem to begin with?
> I have had this macro written for almost a year without this error...so why
> did it start all of a sudden?
>
>
>
> "RichardSchollar" wrote:
> > Hi Tom

>
> > Try placing the following line at the start of your code (after your
> > sub name):

>
> > Application.EnableCancelKey = xlDisabled

>
> > and see if the error goes away.

>
> > Richard

>
> > On 4 Feb, 11:24, Tom <T...@discussions.microsoft.com> wrote:
> > > Hi all,
> > > I have a workbook that has been running fine for a very long time. Last week
> > > I noticed that all of a sudden I am getting an error that states "Code
> > > execution has been interrupted". The macro still completes the operation, but
> > > the error box comes up.
> > > I have not altered the code in any way for quite some time, but this error
> > > mysteriously started happening last week seemingly out of the blue. Any clues
> > > as to what it could be and fixes for this?

>
> > > Public Sub Print_Visible_Worksheets()
> > > * * Application.ScreenUpdating = False
> > > * * Dim sh As Worksheet
> > > * * Dim arr() As String
> > > * * Dim N As Long
> > > * * Const sStr As String = "Main"

>
> > > * * For Each sh In ThisWorkbook.Worksheets
> > > * * * * With sh
> > > * * * * * * If .Visible = xlSheetVisible Then
> > > * * * * * * * * If StrComp(.Name, sStr, vbTextCompare)Then
> > > * * * * * * * * * * N = N + 1
> > > * * * * * * * * * * ReDim Preserve arr(1 To N)
> > > * * * * * * * * * * arr(N) = .Name
> > > * * * * * * * * End If
> > > * * * * * * End If
> > > * * * * End With
> > > * * Next sh

>
> > > * * With ThisWorkbook
> > > * * * * .Worksheets(arr).PrintOut
> > > * * * * .Worksheets(1).Select *<-----ERROR IS ON THIS LINE
> > > * * End With
> > > * * Application.ScreenUpdating = True
> > > End Sub- Hide quoted text -

>
> - Show quoted text -


 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      4th Feb 2008
I guess I'll just put it under the "strange but true" category and just
accept it.
Thanks for the fix to the problem Richard...it's very much appreciated.

"RichardSchollar" wrote:

> Tom
>
> I'm afraid I can't answer that - there's nothing wrong with your
> code. It seems to be an Excel bug, and disabling the cancel key seems
> to be a workaround for it.
>
> Sorry I can't be any more helpful than that!
>
> Richard
>
>
> On 4 Feb, 11:55, Tom <T...@discussions.microsoft.com> wrote:
> > Hi Richard,
> > That seems to fix the problem. Now, in order that I can understand
> > this....WHY would this fix the problem...and why, all of a sudden, would it
> > start causing a problem to begin with?
> > I have had this macro written for almost a year without this error...so why
> > did it start all of a sudden?
> >
> >
> >
> > "RichardSchollar" wrote:
> > > Hi Tom

> >
> > > Try placing the following line at the start of your code (after your
> > > sub name):

> >
> > > Application.EnableCancelKey = xlDisabled

> >
> > > and see if the error goes away.

> >
> > > Richard

> >
> > > On 4 Feb, 11:24, Tom <T...@discussions.microsoft.com> wrote:
> > > > Hi all,
> > > > I have a workbook that has been running fine for a very long time. Last week
> > > > I noticed that all of a sudden I am getting an error that states "Code
> > > > execution has been interrupted". The macro still completes the operation, but
> > > > the error box comes up.
> > > > I have not altered the code in any way for quite some time, but this error
> > > > mysteriously started happening last week seemingly out of the blue. Any clues
> > > > as to what it could be and fixes for this?

> >
> > > > Public Sub Print_Visible_Worksheets()
> > > > Application.ScreenUpdating = False
> > > > Dim sh As Worksheet
> > > > Dim arr() As String
> > > > Dim N As Long
> > > > Const sStr As String = "Main"

> >
> > > > For Each sh In ThisWorkbook.Worksheets
> > > > With sh
> > > > If .Visible = xlSheetVisible Then
> > > > If StrComp(.Name, sStr, vbTextCompare) Then
> > > > N = N + 1
> > > > ReDim Preserve arr(1 To N)
> > > > arr(N) = .Name
> > > > End If
> > > > End If
> > > > End With
> > > > Next sh

> >
> > > > With ThisWorkbook
> > > > .Worksheets(arr).PrintOut
> > > > .Worksheets(1).Select <-----ERROR IS ON THIS LINE
> > > > End With
> > > > Application.ScreenUpdating = True
> > > > End Sub- Hide quoted text -

> >
> > - Show quoted text -

>
>

 
Reply With Quote
 
CAROLYN
Guest
Posts: n/a
 
      21st Oct 2008
MANY THANKS TOM, BUT WHERE DO I GO TO SET THIS CODE - SORRY BUT I'M NOT AS
COMPUTER LITERATE AS YOU ARE UNFORTUNATELY.

"Tom" wrote:

> Hi all,
> I have a workbook that has been running fine for a very long time. Last week
> I noticed that all of a sudden I am getting an error that states "Code
> execution has been interrupted". The macro still completes the operation, but
> the error box comes up.
> I have not altered the code in any way for quite some time, but this error
> mysteriously started happening last week seemingly out of the blue. Any clues
> as to what it could be and fixes for this?
>
> Public Sub Print_Visible_Worksheets()
> Application.ScreenUpdating = False
> Dim sh As Worksheet
> Dim arr() As String
> Dim N As Long
> Const sStr As String = "Main"
>
> For Each sh In ThisWorkbook.Worksheets
> With sh
> If .Visible = xlSheetVisible Then
> If StrComp(.Name, sStr, vbTextCompare) Then
> N = N + 1
> ReDim Preserve arr(1 To N)
> arr(N) = .Name
> End If
> End If
> End With
> Next sh
>
> With ThisWorkbook
> .Worksheets(arr).PrintOut
> .Worksheets(1).Select <-----ERROR IS ON THIS LINE
> End With
> Application.ScreenUpdating = True
> End Sub

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      21st Oct 2008
If you're new to macros:

Debra Dalgleish has some notes how to implement macros here:
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

(General, Regular and Standard modules all describe the same thing.)

CAROLYN wrote:
>
> MANY THANKS TOM, BUT WHERE DO I GO TO SET THIS CODE - SORRY BUT I'M NOT AS
> COMPUTER LITERATE AS YOU ARE UNFORTUNATELY.
>
> "Tom" wrote:
>
> > Hi all,
> > I have a workbook that has been running fine for a very long time. Last week
> > I noticed that all of a sudden I am getting an error that states "Code
> > execution has been interrupted". The macro still completes the operation, but
> > the error box comes up.
> > I have not altered the code in any way for quite some time, but this error
> > mysteriously started happening last week seemingly out of the blue. Any clues
> > as to what it could be and fixes for this?
> >
> > Public Sub Print_Visible_Worksheets()
> > Application.ScreenUpdating = False
> > Dim sh As Worksheet
> > Dim arr() As String
> > Dim N As Long
> > Const sStr As String = "Main"
> >
> > For Each sh In ThisWorkbook.Worksheets
> > With sh
> > If .Visible = xlSheetVisible Then
> > If StrComp(.Name, sStr, vbTextCompare) Then
> > N = N + 1
> > ReDim Preserve arr(1 To N)
> > arr(N) = .Name
> > End If
> > End If
> > End With
> > Next sh
> >
> > With ThisWorkbook
> > .Worksheets(arr).PrintOut
> > .Worksheets(1).Select <-----ERROR IS ON THIS LINE
> > End With
> > Application.ScreenUpdating = True
> > End Sub


--

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
Error 400 in VB code execution? CellShocked Microsoft Excel Programming 1 28th Nov 2009 10:14 AM
Code Execution Error =?Utf-8?B?Q3JhaWc=?= Microsoft Excel Programming 1 29th Aug 2006 09:28 AM
Macro 'Code execution has been interupted' error after print? =?Utf-8?B?Qmx1ZWhhaXIgdGhlIFBpcmF0ZQ==?= Microsoft Excel Programming 1 9th Feb 2006 06:19 PM
Re: error: Code Execution has been interrupted Joshua Fredrickson Microsoft Excel Programming 1 23rd Nov 2005 03:44 PM
code runs well on 3rd execution after getting Error: 3021 on 1st 2 execution =?Utf-8?B?eWFubg==?= Microsoft Access VBA Modules 0 23rd Mar 2004 10:01 AM


Features
 

Advertising
 

Newsgroups
 


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