Excel vba脚本:find()在第二次迭代时不返回任何内容

时间:2015-08-03 06:26:18

标签: excel vba excel-vba find

For active_row = 9 To last_row

    ws1_func_loc = ThisWorkbook.Sheets(ws1).Cells(active_row, "C").Value
    ws1_mat_id = ThisWorkbook.Sheets(ws1).Cells(active_row, "D").Value
    ws1_mat_qty = ThisWorkbook.Sheets(ws1).Cells(active_row, "I").Value
    ws1_reason2 = ""


    zc_sum = WorksheetFunction.SumIfs(ThisWorkbook.Sheets(ws2).Range("F:F"), ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc, ThisWorkbook.Sheets(ws2).Range("N:N"), ws1_mat_id, ThisWorkbook.Sheets(ws2).Range("S:S"), "ZC")
    zk_sum = WorksheetFunction.SumIfs(ThisWorkbook.Sheets(ws2).Range("F:F"), ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc, ThisWorkbook.Sheets(ws2).Range("N:N"), ws1_mat_id, ThisWorkbook.Sheets(ws2).Range("S:S"), "ZK")


    'some other If conditions...


    ElseIf zc_sum = 0 And zk_sum > 0 Then

        row_match_count = WorksheetFunction.CountIf(ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc)

        Set found = ThisWorkbook.Sheets(ws2).Range("K:K").Find(What:=ws1_func_loc) 'find() found nothing on 2nd iteration

        For i = 1 To row_match_count

            If ThisWorkbook.Sheets(ws2).Cells(found.Row, "N").Value = ws1_mat_id And ThisWorkbook.Sheets(ws2).Cells(found.Row, "S") = "ZK" And Not found Is Nothing Then

                 ws1_reason2 = ws1_reason2 & Chr(10) & ThisWorkbook.Sheets(ws2).Cells(found.Row, "R").Value & ", " & "qty " & ThisWorkbook.Sheets(ws2).Cells(found.Row, "F").Value & ", " & ThisWorkbook.Sheets(ws2).Cells(found.Row, "U").Value & ", " & ThisWorkbook.Sheets(ws2).Cells(found.Row, "W").Value

                 Set found = Sheets(ws2).Range("K:K").FindNext(After:=found)

        Else

                Set found = Sheets(ws2).Range("K:K").FindNext(After:=found)

        End If

    Next i

    ThisWorkbook.Sheets(ws1).Cells(active_row, "O").Value = ws1_reason2


    ElseIf .......

我正在尝试进行多标准搜索,我发现工作表2中的所有行(ws2)与工作表1(ws2)中特定行的值相匹配。

它在第一次For循环迭代期间工作,即active_row = 9,但在第二次迭代期间,即active_row = 10时,Set found = ThisWorkbook.Sheets(ws2).Range("K:K").Find(What:=ws1_func_loc)返回Nothing。< / p>

但肯定至少有匹配,因为row_match_count会出现一个大于0的值。

1 个答案:

答案 0 :(得分:0)

我终于找到了什么错误。

ws2中有一堆下拉列表会过滤结果。

如果我使用下拉列表过滤行,ThisWorkbook.Sheets(ws2).Range("K:K").Find(What:=ws1_func_loc)将只能从过滤结果中查看/查找,而WorksheetFunction.CountIf(ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc)仍然可以查看整个工作表。