0

I am creating an Excel file using C#.

The end result should be:

  1. Excel workbook with 5 sheets

  2. 1 sheet is generated using other 4 sheets.

  3. 4 sheets are generated from database

  4. All the formulas should be added to sheet 1 just in case the user wants to add data manually to other 4 sheets

I am stuck at:

After I create all the 4 sheets, I am trying to add formulae to sheet 1.

Formula Logic - Look for value of column A (all rows one by one) of sheet 1 (current) in sheet 2's column A, and get the value of column K of that row.

I am trying to add the below VLOOKUP to the 12th row and will copy the formula to all rows.

oRng = worksheet.Range["J12"];
oRng.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(RC[-9],'Sheet2'!$A:$K,11,0)),'',VLOOKUP(RC[-9],'Sheet2'!$A:$K,11,0)";   

But I get the error:

Exception from HRESULT: 0x800A03EC

4

2 回答 2

1

我相信你是:

  • 混合 A1 和 R1C1 风格(它不喜欢)
  • 使用''而不是""(它不喜欢)
  • 用于$A1 样式引用的绝对引用FormulaR1C1(它不喜欢)

编辑在您的情况下似乎不太可能,因为您已经说过该公式适用于直接输入,但您可能还有一个语言环境问题,其中 excel 需要分号列表分隔符并且您正在使用逗号,请参阅我的答案以获取解决方案。(结束编辑)

所以试试:

oRng.Formula = @"=IF(ISERROR(VLOOKUP(A12,Sheet2!$A:$K,11,0)),"""",VLOOKUP(A12,Sheet2!$A:$K,11,0))";

或者

oRng.FormulaR1C1 = @"=IF(ISERROR(VLOOKUP(RC[-9],Sheet2!C1:C11,11,0)),"""",VLOOKUP(RC[-9],Sheet2!C1:C11,11,0))";

(如果您不想为字符串添加前缀,@则将所有内容更改""\"字符串内部)

脚注

您可以在更高版本的 Excel(2007 及更高版本)中将公式缩短为此:

oRng.Formula     = @"=IFERROR(VLOOKUP(A12,Sheet2!$A:$K,11,0),"""")"
oRng.FormulaR1C1 = @"=IFERROR(VLOOKUP(RC[-9],Sheet2!C1:C11,11,0),"""")"
于 2013-05-23T18:53:38.523 回答
0

在 Excel 公式中输入正确数量的括号:最后缺少一个。

于 2013-05-10T13:18:37.067 回答