Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 201
Default Ignore macro run time error

Iget the following error message

"Run Time error 1004
Unable to get the legendentries property of the legend class"

This occurs when users enter invalid parameters for a query that fetches
data for a pivot table, among other stuff.
Can the macro ignore the error so users are not tempted to debug the macro
code? The macro can then complete and the user can input valid parameters.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,533
Default Ignore macro run time error

Hi Phil

You need to use the bulit-in error handler.

Below is an example of how it can be done.

Sub Test()
'User enter parameters
On Error GoTo ErrHandler
' Code that throws error
On Error GoTo 0 ' Stop error handler
'Rest of your code
Exit Sub ' Stop before ErrHandler

ErrHandler:
msg = MsgBox("Invalid parameters, macro abort.", vbCritical, "Best regards,
Per Jessen")
End Sub

Regards,

Per
"phil" skrev i meddelelsen
...
Iget the following error message

"Run Time error 1004
Unable to get the legendentries property of the legend class"

This occurs when users enter invalid parameters for a query that fetches
data for a pivot table, among other stuff.
Can the macro ignore the error so users are not tempted to debug the
macro
code? The macro can then complete and the user can input valid
parameters.


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 201
Default Ignore macro run time error

Per,
Thanks for you answer but this is where the error happens in the code

"ActiveChart.Legend.LegendEntries(5).LegendKey.Sel ect"

There is no legend because there is no data to fetch and it will happen at
each legend entry x5. (I had to write this part of the macro to format the
legend to the preffered layout from default after the pivot refreshed.)

Which part of the code is going in above or below the place where the error
occurs to stop the error message and allow the macro to complete without
doing the formatting?

"Per Jessen" wrote:

Hi Phil

You need to use the bulit-in error handler.

Below is an example of how it can be done.

Sub Test()
'User enter parameters
On Error GoTo ErrHandler
' Code that throws error
On Error GoTo 0 ' Stop error handler
'Rest of your code
Exit Sub ' Stop before ErrHandler

ErrHandler:
msg = MsgBox("Invalid parameters, macro abort.", vbCritical, "Best regards,
Per Jessen")
End Sub

Regards,

Per


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,533
Default Ignore macro run time error

Phil,

Given the new information, this is how to handle it.

On Error Resume Next ' Ignore lines that throws error (after this statement)
Activechart.Legend.LegendEntries....
' Code to set other legends
On error Goto 0 ' Disable the Error Handling
' Continue with other code

Regards,

Per
"phil" skrev i meddelelsen
...
Per,
Thanks for you answer but this is where the error happens in the code
"ActiveChart.Legend.LegendEntries(5).LegendKey.Sel ect"


There is no legend because there is no data to fetch and it will happen at
each legend entry x5. (I had to write this part of the macro to format the
legend to the preffered layout from default after the pivot refreshed.)

Which part of the code is going in above or below the place where the
error
occurs to stop the error message and allow the macro to complete without
doing the formatting?

"Per Jessen" wrote:

Hi Phil

You need to use the bulit-in error handler.

Below is an example of how it can be done.

Sub Test()
'User enter parameters
On Error GoTo ErrHandler
' Code that throws error
On Error GoTo 0 ' Stop error handler
'Rest of your code
Exit Sub ' Stop before ErrHandler

ErrHandler:
msg = MsgBox("Invalid parameters, macro abort.", vbCritical, "Best
regards,
Per Jessen")
End Sub

Regards,

Per



  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 201
Default Ignore macro run time error

Excellent, thank you Per

"Per Jessen" wrote:

Phil,

Given the new information, this is how to handle it.

On Error Resume Next ' Ignore lines that throws error (after this statement)
Activechart.Legend.LegendEntries....
' Code to set other legends
On error Goto 0 ' Disable the Error Handling
' Continue with other code

Regards,

Per
"phil" skrev i meddelelsen
...
Per,
Thanks for you answer but this is where the error happens in the code
"ActiveChart.Legend.LegendEntries(5).LegendKey.Sel ect"


There is no legend because there is no data to fetch and it will happen at
each legend entry x5. (I had to write this part of the macro to format the
legend to the preffered layout from default after the pivot refreshed.)

Which part of the code is going in above or below the place where the
error
occurs to stop the error message and allow the macro to complete without
doing the formatting?

"Per Jessen" wrote:

Hi Phil

You need to use the bulit-in error handler.

Below is an example of how it can be done.

Sub Test()
'User enter parameters
On Error GoTo ErrHandler
' Code that throws error
On Error GoTo 0 ' Stop error handler
'Rest of your code
Exit Sub ' Stop before ErrHandler

ErrHandler:
msg = MsgBox("Invalid parameters, macro abort.", vbCritical, "Best
regards,
Per Jessen")
End Sub

Regards,

Per




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
MACRO Run Time Error 1004 The Toasterman Excel Discussion (Misc queries) 1 June 30th 06 04:22 PM
Macro Run-time error shirley_kee Excel Discussion (Misc queries) 1 May 3rd 06 09:50 PM
Macro Issue - Run time error 1004 Matt7102 Excel Discussion (Misc queries) 1 December 22nd 05 12:35 AM
Macro time out error mike b Excel Worksheet Functions 1 December 10th 05 05:21 PM
Visual Basic macro run time error(13) type mismatch Paul Excel Discussion (Misc queries) 0 October 25th 05 07:28 AM


All times are GMT +1. The time now is 09:54 AM.

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"