Delphi和Excel – 使用自动填充

在delphi10 /西雅图,与Excel 2013 …我试图:

  • 添加一列(列AJ),并设置其标题文本(代码工程)
  • 将列2的值设置为AutoSum(代码有效)
  • 将第2行的计算复制到所有剩余的行。 (不起作用)

在“复制计算”步骤中,我使用自动填充方法。 我收到一个错误“范围类失败的自动填充方法”。 但我不知道为什么…(注意variablesaws是全局设置为活动工作表的。)

const TOTAL_TOUCHES = 'AJ'; ... var ColumnHeader : OleVariant; SecondRow : OleVariant; ColRange : OleVariant; myAddr, myAddr2 : String; LastCell : string; begin // This routine adds the header 'Total Touches' and then puts an AutoSum in that column for all rows // Set the header myAddr := TOTAL_TOUCHES + '1'; ColumnHeader := aws.Range[ myAddr, myAddr]; ColumnHeader.FormulaR1C1 := 'Total Touches'; // Put the first occurance of the Autosum in Row 2 myAddr := TOTAL_TOUCHES + '2'; SecondRow := aws.Range[ myAddr, myAddr]; SecondRow.FormulaR1C1 := '=SUM(RC[-6]:RC[-1])'; SecondRow.Autofill(aws.range['AJ3:AJ50', EmptyParam], xlFillCopy); 

一旦这个工作,我会设置这复制到行中的所有列(而不是AJ3:AJ50,但这是一个婴儿的一步…

我究竟做错了什么?

该文档说明AutoFill的目标参数是第一个参数,“必须包含源范围”。 在你的情况下,源范围是AJ2,但AJ3:AJ50不包括该单元格。