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
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
For 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