Ignore macro run time error

  • Thread starter Thread starter phil
  • Start date Start date
P

phil

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.
 
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
 
Per,
Thanks for you answer but this is where the error happens in the code

"ActiveChart.Legend.LegendEntries(5).LegendKey.Select"

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?
 
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
 
Excellent, thank you Per

Per Jessen said:
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
 
Back
Top