多条件搜索功能不起作用

时间:2017-09-06 13:58:54

标签: excel vba excel-vba

我有一个函数,它接受2个搜索值和2个列和1个结果列(总共5个参数),如果满足前两个条件,则返回最后一列的值。但它不起作用并返回VALUE错误。我无法弄清楚这里有什么问题。

 Function betterSearch(searchValue1 As Variant, searchValue2 As Variant, _
            searchColumn1 As Variant, searchColumn2 As Variant, _
            resultColumn As Variant)
    Dim i As Integer
    For i = 1 To searchColumn1.Rows.Count
        If searchColumn1.Cells(i, 1).Value = searchValue1 _
                    And searchColumn2.Cells(i, 2).Value = searchValue2 Then
            betterSearch = resultColumn.Cells(i, 1)
                MsgBox ("found")
                Exit For
        End If
        betterSearch = "Not found"
    Next
End Function

2 个答案:

答案 0 :(得分:2)

这对我很有用 - 的更新

 Sub foo44()
    Dim val As Variant
    Dim c1 As Range
    Dim c2 As Range
    Dim c3 As Range
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set c1 = ws.Range("C:C")
    Set c2 = ws.Range("D:D")
    Set c3 = ws.Range("E:E")

    Debug.Print betterSearch(23, 23, c1, c2, c3)
    End Sub


     Function betterSearch(searchValue1 As Variant, searchValue2 As Variant, searchColumn1 As Range, searchColumn2 As Range, resultColumn As Range)
        Dim i As Long
        Dim c1 As Variant
        Dim c2 As Variant
        For i = 1 To searchColumn1.End(xlDown).Row
             c1 = searchColumn1.Cells(i, 1).Value
             c2 = searchColumn2.Cells(i, 1).Value
            ' Debug.Print c1
             'Debug.Print c2
            If c1 = searchValue1 And c2 = searchValue2 Then
                betterSearch = resultColumn.Cells(i, 1).Value
                    Exit For
            End If
            betterSearch = "Not found"
        Next
    End Function

答案 1 :(得分:0)

尝试获取具有class Example { for( $i=0; $i<5; $i++ ) { public $num . $i; } } 属性的列号。同时尝试按以下方式获取最后使用的行号:

.column