0

我有一行数据,它下面的一行单元格都有#REF!错误。我想使用以下命令将数据行向下拖动到错误行上:

Excel.Range topLeft=ReportSheet.Cells[1,1];
Excel.Range topRight=ReportSheet.Cells[1,10];
Excel.Range bottomRight=ReportSheet.Cells[3,10];
Excel.Range sourceRange = ReportSheet.Cells[topLeft, topRight];
Excel.Range targetRange = ReportSheet.Cells[topLeft, bottomRight];
sourceRange.AutoFill(targetRange);

sourceRange分配没有问题,但分配targetRange(包括带有 #REF! 错误的行)会引发 COM 异常:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Exception from HRESULT: 0x800A03EC

关于如何避免此异常的任何想法?

更新:我尝试删除#REF!尝试执行自动填充之前的行。同样的错误。 sourceRange.AutoFill在数据下方没有错误行的测试数据上工作正常。

4

2 回答 2

0
Excel.Range temp1 =ReportSheet.Cells[1,1];
Excel.Range temp2 =ReportSheet.Cells[1,2];
Excel.Range temp3 =ReportSheet.Cells[1,20];
Excel.Range sourceRange = ReportSheet.Cells[temp1 , temp2 ];
Excel.Range targetRange = ReportSheet.Cells[temp1 , temp3];
sourceRange.AutoFill(targetRange);

尝试上面它会工作。

自动填充的目标必须包括源范围。

于 2013-06-01T05:58:47.417 回答
0

我已经解决了这个问题。有两个问题:

  • 设置targetRange要求使用ReportSheet.Range而不是ReportSheet.Cells
  • AutoFill仅适用于一维,即填充其他行的行或填充其他列的列。我认为我的原始代码失败了,因为设置targetRangeRange[topLeft,bottomRight]意味着AutoFill必须填写正确。

修改后的工作代码是:

Excel.Range topLeft=ReportSheet.Cells[1,1];
Excel.Range bottomLeft=ReportSheet.Cells[3,1];
Excel.Range topRight=ReportSheet.Cells[1,10];
Excel.Range bottomRight=ReportSheet.Cells[3,10];
Excel.Range topRow=ReportSheet.Range[topLeft,topRight];
Excel.Range bottomRow=ReportSheet.Range[bottomLeft,bottomRight];
Excel.Range targetRange=ReportSheet.Range[topRow,bottomRow];
topRow.AutoFill(targetRange);

我从这个答案中得到了线索。

于 2013-06-06T00:53:32.947 回答