Ich bin dabei, eine VBA-Prozedur in ein C# -Projekt zu portieren. Ich bin kurz davor, alles komplett fertig zu haben, aber ich habe noch 2 Fehler. Dies ist die Syntax:C# Verwenden der Excel-Suchfunktion
public static void ExcelPort()
{
object What = "*";
object After = xlWorkSheet.get_Range("A1", "IV65536");
object LookIn = Excel.XlFindLookIn.xlValues;
object LookAt = Excel.XlLookAt.xlPart;
object SearchOrder = Excel.XlSearchOrder.xlByRows;
Excel.XlSearchDirection SearchDirection = Excel.XlSearchDirection.xlNext;
object MatchCase = System.Reflection.Missing.Value;
object MatchByte = System.Reflection.Missing.Value;
object SearchFormat = System.Reflection.Missing.Value;
currentsheet = (Excel._Worksheet)(xlWorkBook.ActiveSheet);
activeworkbook = (Excel.Workbook)xlApp.ActiveWorkbook;
int lastrow = 0;
foreach (Excel.Worksheet sheet in activeworkbook.Worksheets)
{
if (usedrows = (double)xlApp.WorksheetFunction.CountA(sheet.Cells) != 0)
{
lastrow = sheet.Cells.Find(What,After,LookIn,LookAt,SearchOrder,SearchDirection,MatchCase,MatchByte,SearchFormat);
}
else
{
lastrow = 1;
}
}
}
Fehlerliste & Linien
//This line throws an error
(double)xlApp.WorksheetFunction.CountA(sheet.Cells) != 0
//Error:
Cannot implicitly convert type 'bool' to 'double'
//This line throws an error sheet.Cells.Find(What,After,LookIn,LookAt,SearchOrder,SearchDirection,MatchCase,MatchByte,SearchFormat)
//Error:
Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Range' to 'int'
@stuartd - wenn ich die Zeile ändern, um if (usedrows = (double) (xlApp.WorksheetFunction.CountA (sheet.Cells))! = 0) Ich bekomme immer noch den gleichen Fehler –
@stuartd - double usedrows; –
Find() gibt einen Verweis auf die übereinstimmende Zelle zurück (wenn es exIst) oder Nothing/Null, wenn keine Übereinstimmung gefunden wird. 'lastrow = sheet.Cells.Find (...). Row ist wahrscheinlich, was du hier willst. –