Right now I'm writing the code for the output of an app I developed with the appdesigner, and I'm struggling to make it create a chart using Actx. I'm getting a strange error saying that the method for creating a chart cannot be found :/ Other than that all the commands that I've sent to excel are working just fine....
Line of code used:
Chart=wb.ActiveSheet.ChartObjects.Add();
Any idea what's happening?
Thanks a lot in advance.

 채택된 답변

Guillaume
Guillaume 2017년 11월 9일

0 개 추천

A lot of VBA examples on the net use Activexxx to refer to various objects. It always befuddles me since that breaks easily (something else, maybe the user, may change what is actually active).
Possibly, the ActiveSheet is not a worksheet to which a Chart object can be added. Rather than using ActiveSheet get the reference to the sheet you actually want to use:
ws = wb.Sheets.Item(sheetnameornumber);
chart = ws.ChartObjects.Add
If that still doesn't work, then I would break down the expression to see which part actually fails:
worksheets = wb.Worksheets; %get the worksheet collection
ws = worksheets.Item(nameorindex); %get a specific worksheet
chartobjects = ws.ChartObjects; %get the chartobject collection of the worksheet
chart = chartobjects.Add; %add a chart

댓글 수: 3

Eduardo Gaona Peña
Eduardo Gaona Peña 2017년 11월 9일
I just found the problem. No idea why but my excel works with a method called "AddChart2"...................................................................... I literally hate vba....
Guillaume
Guillaume 2017년 11월 9일
AddChart2 is not a method of the Worksheet object. It is a method of the Shapes collection which is a completely different collection from the ChartObjects collection.
Mike Shives
Mike Shives 2023년 8월 15일
I just tried using ws.ChartObjects.Add and got the message ''Add' requires Model-Based Calibration Toolbox.' any advice?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

질문:

2017년 11월 9일

댓글:

2023년 8월 15일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by