2016-12-18 2 views
0

VBAプロシージャをC#プロジェクトに移植しています。私はすべてが終わったばかりだが、2つのエラーが残っている。これは、構文は次のとおりです。C#Excel検索機能の使用

あなたの最初のエラーのために
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; 
    } 
} 
} 

エラー一覧&行

//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' 
+0

@stuartd - 私は場合に行を変更した場合(usedrowsの=(ダブル)( xlApp.WorksheetFunction.CountA(sheet.Cells))!= 0)私はまだ同じエラーを受け取ります。 –

+0

@stuartd - double usedrows; –

+0

Find()は、一致するセル(存在する場合)への参照を返します。一致しない場合はNothing/Nullを返します。 'lastrow = sheet.Cells.Find(...)。Row'はおそらくあなたがここで欲しいものです。 –

答えて

0

、これは単に間違ってC#構文であるとExcelとは何の関係もありません:

if (usedrows = (double)xlApp.WorksheetFunction.CountA(sheet.Cells) != 0) 

it boolである!= 0の比較の結果をusedrowsに割り当てようとすると文句を言う。これはdoubleである。 0への割り当ての結果を比較するために括弧を使用する:

if ((usedrows = (double)xlApp.WorksheetFunction.CountA(sheet.Cells)) != 0) 

第2のエラーが.FindExcel.Range、ないintを返すことです。 VBAでは、非参照型に割り当てる場合は、参照型の既定のメンバを呼び出します。 C#はそれをしません - あなたは明示的な呼び出しを行う必要があります。 Rangeのデフォルトメンバーは.Valueですが、文脈によって、私はあなたが行番号を探していると仮定しています:

var found = sheet.Cells.Find(What, After, LookIn, LookAt, SearchOrder, 
    SearchDirection, MatchCase, MatchByte, SearchFormat); 
lastrow = found != null ? found.Row : 1; 
+0

@Comintrn - あなたはあなたの仮定に合っています、私は行番号を探していますので、それに1を加えて "合計"行を追加することができます。 –

+0

あなたの行にlastrow = found!= null? found.Row:1; - 「Microsoft.Office.Interop.Excel.Range」に暗黙的に 'int'型を変換できません。 –

+0

@StarsFlyFreeFromCozyNights - そのエラーはまったく複製できません。あなたは 'int lastrow = 0;'宣言を変更しましたか? – Comintern