SYMPTOMS
In Microsoft Excel, you may receive the following error message when
you run a Visual Basic, Applications Edition, procedure that contains the
ChartWizard method:
Run-time error '1004':
External reference is not valid
CAUSE
This problem occurs if the Source argument that you use with the
ChartWizard method contains a number of nonadjacent range references.
Note that the number of the range references that will cause this
error message depends on the length of the name of the worksheet that
the chart is created on.
For example, the following procedure correctly creates a chart on the
active sheet, Sheet1, (assuming that the range A1:B14 on Sheet1
contains data):
Sub CreateChart()
ActiveSheet.ChartObjects.Add(100, 40, 300, 200).Select
Application.CutCopyMode = False
ActiveChart.ChartWizard Source:= Range( _
"A1:B1,A2:B2,A3:B3,A4:B4,A5:B5,A6:B6,A7:B7," _
& "A8:B8,A9:B9,A10:B10,A11:B11,A12:B12,A13:B13")
End Sub
If you add another reference in the Range object for the Source argument,
you receive the error message described above when you run the procedure.
This problem occurs because Visual Basic converts the range string
("A1:C1,A2:C2,...") into a parsed reference. When you use the
ChartWizard method with this reference, the ChartWizard method
converts this reference to a string. When this occurs, the reference
is converted to R1C1 format, and the sheet name is added to each
individual reference. This causes the reference string to exceed the
allotted buffer, and to be cut off. Because the reference is cut off,
you receive the error message above when the ChartWizard method
attempts to create the chart.
Microsoft provides examples of Visual Basic for Applications procedures for
illustration only, without warranty either expressed or implied, including,
but not limited to the implied warranties of merchantability and/or fitness
for a particular purpose. The Visual Basic procedures in this article are
provided 'as is' and Microsoft does not guarantee that they can be used in
all situations. While Microsoft support professionals can help explain the
functionality of a particular macro, they will not modify these examples to
provide added functionality, nor will they help you construct macros to
meet your specific needs. If you have limited programming experience, you
may want to consult one of the Microsoft Solution Providers. Solution
Providers offer a wide range of fee-based services, including creating
custom macros. For more information about Microsoft Solution Providers,
call Microsoft Customer Information Service at (800) 426-9400.
RESOLUTION
To avoid receiving the error message above, use one of the following
methods.
Method 1: Use the MS Excel 4.0 Macro equivalent, CHART.WIZARD().
The CHART.WIZARD() function works correctly with nonadjacent cell
references because the range remains a string and does not need
to be converted. The original string that you use as the Ref
argument in the CHART.WIZARD function is the string that is
parsed.
Method 2: Copy and paste the cells in the range from which you are creating the chart so that they appear as one contiguous range. This range
can then be referred to as one single range in the ChartWizard
method.