使用excel范围查找返回类型不匹配。 (HRESULT异常:0x80020005(DISP_E_TYPEMISMATCH))

时间:2014-05-28 10:29:35

标签: vb.net excel visual-studio-2013

我正在使用Excel插件 我需要在Column(用户选择)中搜索单元格上的值(由用户选择),并在单元格中写入结果(由用户选择)

例如:如果第5行,第10行和第15行的B列中存在单元格A2导致C2必须为5,10,15

尝试使用此代码查找Row时出现错误

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

我收到错误类型不匹配。 (尝试将行号分配给结果变量时,HRESULT异常:0x80020005(DISP_E_TYPEMISMATCH))

1 个答案:

答案 0 :(得分:0)

我临时将我的代码更改为以下代码,它与我合作

   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