View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
vbaprog vbaprog is offline
external usenet poster
 
Posts: 13
Default Charting - Cells and Range - Error 1004

Hi,

I want to create a chart that is plotting data from two different
columns. The column names and the length of the columns are determined by
other data in the program which vary according to the user input.

The last line of the code generates Runtime error 1004:
Application-defined or object-defined error. The other code with single quote
also generates the same error. What I need is a good replacement for the
statement below but with the flexibility to define the columns and their
length programatically.

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A16:A19,C16:C19"),
PlotBy _
:=xlColumns

The error with my code is with defining the range becoz when I place
the cursor on the cells(row1,col1) in the 8th line (setting range1) this is
what is displayed.
cells(row1,col1) = <Method 'Cells' of object '_Global' failed. Any help is
very much appreciated.

My code
----------------------------------------------------------------------------------------------
Sub ChartCreation()
Dim row1, row2, col1, col2 As Integer 'Values are determined by other
data in
the program
Dim range1, range2, myRange As Range
row1 = 16
row2 = 19
col1 = 1
col2 = 3

Set range1 = Range(Cells(row1, col1), Cells(row2, col1))
Set range2 = Range(Cells(row1, col2), Cells(row2, col2))
Set myRange = Union(range1, range2)
Sheet1.Select
'Set myRange = Sheets("Sheet1").Range("A16:A19,C16:C19") ' This also


generates the

same error
Charts.Add
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range(myRange),
PlotBy _
:=xlColumns
End Su
------------------------------------------------------------------------------------------------
TIA,