简体   繁体   中英

#value! error in cells containing user defined functions

I need to open an Excel workbook using C# as read-only. No problems there. But if that workbook was saved with autofilter on, I need to turn it off using c# so I can read the worksheet values into my class. The problem is, if a cell has a user-defined function in it, that cell will show the #VALUE. error when autofilter is turned off instead of it's correct value. This is not a problem if I manually turn off autofilter just using Excel. So I know there is no problem with the user-defined functions themselves.

Here's what I am doing:

   Excel.Application app = new Excel.Application();
   Excel.Workbook CIRworkbook;
   var missing = Missing.Value;

   bool openAsReadOnly = true;
   CIRworkbook = (Excel.Workbook)(app.Workbooks.Open(xlsFile, missing, openAsReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing));

   app.get_Range("A:IV", missing).Worksheet.AutoFilterMode = false;
   app.CalculateFull();

The app.CalculateFull() line has no effect on removing the #VALUE. error.

Try using the following syntax to get all the cells that contains values in the sheet regardless of the auto-filter on/off or the actual filter used.

I think this is what you're looking for:

Worksheet currSheet = excelApp.ActiveWorkbook.ActiveSheet;
object[,] objData = (object[,])currSheet.UsedRange.Value2;

the Value2 member holds the underline content of the cell/Range. if you're looking for the visible string displayed to the user, use Text property.

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