Cells.Formula=“=CONCATENATE(…)” Exception 0x800A03EC

こ雲淡風輕ζ 提交于 2019-12-24 16:18:13

问题


I'm writing a VB.NET application that generates an Excel file.

My intention here is to write a particular formula that uses CONCATENATE in a cell.

Now, the following line of code fires the above exception:

0) tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula = 
   "=CONCATENATE(" & Pixels(PixelIndex) & ";Batches!J3)"

The following row does NOT raise the exception. (It's simply the row above without the = at the beginning. The fact that it doesn't raise the exception means that the indexes are used properly; I'll get rid of them in the following passages to ease the notation). Also, if I manually put in Excel an = in front of the very same formula, then the formula gives a correct result (it correctly grabs Batches!J3)

1) tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula = 
   "CONCATENATE(" & Pixels(PixelIndex) & ";Batches!J3)"

The following line also runs without problem:

2) tSheet.Cells(indexes).Formula = "=CONCATENATE(" & Pixels(PixelIndex) & ")"

This line works as well:

3) tSheet.Cells(indexes).Formula = "=CONCATENATE(Batches!J3)"

It seems that only the combination of 2) and 3) raises the exception.

I'm using Visual Studio 2012, Excel 2007, and I'm including Microsoft Excel 12.0 Object Library

Thanks for any help!


回答1:


You haven't mentioned your locale, so just in case Excel is expecting argument separators to be comma (as in the en-UK locale) instead of semicolon (as in de-DE locale) then try:

tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula = 
    "=CONCATENATE(" & Pixels(PixelIndex) & ",Batches!J3)"

(edit) If that does work, then you can use something like the following to make it locale independent:

Dim ci As CultureInfo = System.Globalization.CultureInfo.CurrentCulture
Dim listSep As String = ci.TextInfo.ListSeparator
tSheet.Cells(tIncRow + ItemIndex * PixelIndex + PixelIndex, 2).Formula =
    "=CONCATENATE(" & Pixels(PixelIndex) & listSep & "Batches!J3)"

That just grabs the numeric list separator from the windows culture. It does assume that Excel runs with System.Globalization.CultureInfo.CurrentCulture, see the following two MSDN references for more detailed information on this and the special cases you may encounter:

  • Creating Excel Solutions for Use in Multiple Countries/Regions Using Visual Studio Tools for Office (espcially the "Writing Code to Check Regional Settings" section)
  • Creating Office Solutions for Use in Multiple Countries/Regions

(end edit)

If this doesn't work, then we might need more info - see my comments on your original question.



来源:https://stackoverflow.com/questions/16775138/cells-formula-concatenate-exception-0x800a03ec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!