Kromster
Kromster

Reputation: 7397

How to position element (chart) in Excel to a certain cell?

I have a chart in Excel that I need to position (move) into certain cell.

I'm looking for something along these lines:

procedure TMyExcelClass.MyProcedure;
var
  sheet, chartObject: Variant;
begin
  sheet := fExcel.ActiveWorkBook.Sheets['Some sheet'];
  chartObject := sheet.ChartObjects[1];
  chartObject.Left := <Left of cell "F">
  chartObject.Top := <Top of cell "34">
end;

How do I do position chart object to certain cell? (taking into account, that cells might have different widths and heights)

Upvotes: 3

Views: 957

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21033

Create a range of the cell you want to locate the chart to. Then assign the left and top properties of the range to the chart object.

procedure TMyExcelClass.MyProcedure;
var
  sheet, chartObject, r: Variant;
begin
  sheet := fExcel.ActiveWorkBook.Sheets['Some sheet'];

  r := sheet.Range['F34'];

  chartObject := sheet.ChartObjects[1];
  chartObject.Left := r.Left;
  chartObject.Top := r.Top
end;

Upvotes: 4

Related Questions