简体   繁体   中英

Delphi and Excel - Using Autofill

In Delphi 10 / Seattle, with Excel 2013... I am trying to:

  • Add a Column (column AJ), and set its header text (code works)
  • Set the value of the column, row 2, as an AutoSum (code works)
  • Copy the calculation from row 2 to all remaining rows. (doesn't work)

During the 'Copy the Calculation' step, I am using AutoFill method. I am getting an error 'Autofill Method of Range Class Failed.' but I don't know why... (note that variable aws is globally set the to the Active Worksheet.)

        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);

Once this is working, I will set this to copy to ALL columns in the row (as opposed to AJ3:AJ50, but this is a baby-step...

What am I doing wrong?

The documentation says the destination argument to AutoFill , which is the first argument, "must include the source range." In your case, the source range is AJ2, but AJ3:AJ50 doesn't include that cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM