使用excel范围查找返回types不匹配。 (从HRESULTexception:0x80020005(DISP_E_TYPEMISMATCH))

我正在使用Excel插件,我需要在列(由用户select)中search单元格(由用户select)上的值并将结果写入单元格(由用户select)

例如:如果单元格A2存在于行5,10,15中的列B中,则C2必须是5,10,15

我尝试使用此代码查找行时,面对错误

result = sheetName.Cells.Find(cellVal, SearchRange, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, _ Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, _ Type.Missing, Type.Missing, Type.Missing).Row.ToString 

和这个代码也一样

  result = sheetName.Cells.Find(What:=cellVal, LookIn:=SearchRange.Value, SearchOrder:=SearchRange.Rows, _ SearchDirection:=Excel.XlSearchDirection.xlNext, MatchCase:=False, SearchFormat:=False).ToString 

我的完整代码在这里

 Dim xlapp As Excel.Application = Globals.ThisAddIn.Application Dim sheetName As Excel.Worksheet sheetName = Globals.ThisAddIn.Application.ActiveSheet Dim LastDataRow As Integer = sheetName.Range(cmbCheckDataCol.Text & sheetName.Rows.Count).End(Excel.XlDirection.xlUp).Row Dim LastCheckInRow As Integer = sheetName.Range(cmbCheckRngCol.Text & sheetName.Rows.Count).End(Excel.XlDirection.xlUp).Row Dim SearchRange As Excel.Range = sheetName.Range(cmbCheckRngCol.Text & "2", cmbCheckRngCol.Text & LastCheckInRow.ToString) ' --------------------------------- start check Dim cellVal, result As String For rowNum As Integer = 2 To LastDataRow cellVal = sheetName.Range(cmbCheckDataCol.Text & rowNum.ToString).Value.ToString ' if null will return Error 'result = xlapp.WorksheetFunction.CountIf(SearchRange, cellVal) ' search result = sheetName.Cells.Find(cellVal, SearchRange, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, _ Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, _ Type.Missing, Type.Missing, Type.Missing).Row.ToString 

我得到错误types不匹配。 (从HRESULTexception:0x80020005(DISP_E_TYPEMISMATCH))尝试将行号分配给结果variables

我临时将我的代码更改为下面的代码,它适用于我

  Dim LastDataRow As Integer = sheetName.Range(cmbCheckDataCol.Text & sheetName.Rows.Count).End(Excel.XlDirection.xlUp).Row Dim LastCheckInRow As Integer = sheetName.Range(cmbCheckRngCol.Text & sheetName.Rows.Count).End(Excel.XlDirection.xlUp).Row Dim SearchRange As Excel.Range = sheetName.Range(cmbCheckRngCol.Text & "2", cmbCheckRngCol.Text & LastCheckInRow.ToString) ' --------------------------------- start check Dim cellVal As String = "" Dim result As String = "In Row" Dim Curentfind As Excel.Range = Nothing Dim FirstFind As Excel.Range = Nothing ' for try microsoft For rowNum As Integer = 2 To LastDataRow ' ---- try microsoft Try cellVal = sheetName.Range(cmbCheckDataCol.Text & rowNum.ToString).Value.ToString ' if null will return Error Dim ifExist As Long = xlapp.WorksheetFunction.CountIf(SearchRange, cellVal) If ifExist > 0 Then ' start get rows Curentfind = SearchRange.Find(cellVal, , _ Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _ Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False) Dim fFind As String = Curentfind.Address.ToString ' record find first address to avoid infiniti loop For ce As Integer = 1 To LastCheckInRow result &= ", " & Curentfind.Address.ToString.ToUpper Curentfind = SearchRange.FindNext(Curentfind) If Curentfind.Address.ToString = fFind Then Exit For ' breakloop , find back to first address Next result = Replace(result, "$", "") Else result = "Not Exist" End If Catch ex As Exception result = "Error, No data in Cell " & cmbCheckDataCol.Text.ToUpper & rowNum.ToString End Try ' write result sheetName.Range(cmbCheckResltCol.Text & rowNum.ToString).Value = result result = "In Row" Next