View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default How to embed chart in active worksheet with VBA?

I think you misjudge Tushar. Perhaps there is a misunderstanding in what he
has said added to some frustration on your part. But Tushar does know a lot
more about charting than I do - he is one of the mainstays over in
Excel.charting.

this worked for me:
Sub ABCD()
Dim j As Long, strXlsFile As String
Dim strSheetName As String, xlapp As Application
strXlsFile = "Book3"
strSheetName = "Sheet2"
j = 1
Set xlapp = Application
xlapp.Workbooks(strXlsFile).Charts.Add
xlapp.Workbooks(strXlsFile).ActiveChart.ChartType = xlColumnClustered
xlapp.Workbooks(strXlsFile).ActiveChart.SetSourceD ata Source:= _
xlapp.Workbooks(strXlsFile).Worksheets(j + 1).Range("A1:C" & _
xlapp.Workbooks(strXlsFile).Worksheets(j + _
1).Range("C1").End(xlDown).Row), PlotBy:=xlColumns
xlapp.Workbooks(strXlsFile).ActiveChart.Location _
Whe=xlLocationAsObject, Name:=strSheetName

End Sub

You don't get a chart because your range doesn't point to any data - using
that j+1 to indicate the data sheet would be the source of the problem as my
best guess - it is pointing to the wrong sheet probably.

for example, when you get the blank chart, right click on your blank chart
and select source data, then click in the source box and then navigate to
the proper sheet and highlight your data area and hit enter and the chart
will appear.

Anyway, when the reference pointed to the data, the above code worked fine.

--
Regards,
Tom Ogilvy


--
Regards,
Tom Ogilvy


"deko" wrote in message
om...
Check the response to your other discussion on the same subject titled
'Chart location and source...' Among other tips, it contains
information about which two lines generated by the macro recorder have
to be swapped to make the code work.

As I continue to mention, the macro recorder is a good starting point.
However, it is not perfect.


Apparently you don't know what you're talking about.

In the previous post the order was Add.Location, type, source. Switching
type and source would be Add.Location, source, type. The macro recorded
yields type, source, location.

If your previous bad advice had worked, I would not sill be posting about
this probelm.