Microsoft KB Archive/832166

= You receive a &quot;Run-time Error 1004&quot; error message when you make changes to legend entries in an chart in Excel =

Article ID: 832166

Article Last Modified on 8/13/2007

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition

-



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 &quot;Symptoms&quot; 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 site

https://solutionfinder.microsoft.com/

For additional information about the support options available from Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;[LN;CNTACTMS]

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(&quot;Sheet1&quot;).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

Additional query words: XL2007 XL2003 XL2002 XL

Keywords: kberrmsg kbchart kbprogramming kbprb kbpending kbcode kbvba KB832166

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.