You receive a "Run-time Error 1004" error message when you make changes to legend entries in an chart in Excel (832166)



The information in this article applies to:

  • Microsoft Office Excel 2003
  • Microsoft Excel 2002
  • Microsoft Excel 2000

SYMPTOMS

When you run a Microsoft Visual Basic for Applications (VBA) macro that uses the LegendEntries method to make changes to legend entries in a Microsoft Excel chart, you may receive the following error message:
Run-time error '1004': Application or object-defined error

CAUSE

This behavior occurs when the Excel chart contains more legend entries than there is space available to display the legend entries on the Excel chart. When this behavior occurs, Microsoft Excel may truncate the legend entries.

Because the LegendEntries method in your VBA macro uses what appears for the legend (in this case, the truncated legend entries), the error message that is mentioned in the "Symptoms" section of this article occurs when there are more entries than there is space available to display the legend entries on the Excel chart.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and the tools that are used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, visit the following Microsoft Web siteFor additional information about the support options available from Microsoft, visit the following Microsoft Web site: To work around this behavior, create a macro that reduces the font size of the Excel chart legend text before your VBA macro makes changes to the chart legend and then restore the font size of the chart legend so that it is similar to the following macro example.

Note You must have an Excel chart on your worksheet for this macro to run correctly.
Sub ResizeLegendEntries()

   With Worksheets("Sheet1").ChartObjects(1).Activate
      ' Store the current font size
      fntSZ = ActiveChart.Legend.Font.Size
 
      'Temporarily change the font size.
      ActiveChart.Legend.Font.Size = 2
 
         'Place your LegendEntries macro code here to make
         'the changes that you want to the chart legend.
 
      ' Restore the font size.
      ActiveChart.Legend.Font.Size = fntSZ
   End With

End Sub

Modification Type:MajorLast Reviewed:12/15/2005
Keywords:kbProgramming kbchart KbVBA kberrmsg kbpending kbcode kbprb KB832166 kbAudEndUser kbAudDeveloper