第二次通话后退出搜索功能

时间:2018-10-02 18:59:11

标签: excel vba excel-vba

我有一个简单的搜索功能-fold -1 <<<"$data" -它被多次调用以返回行号。

在前两个调用(调用1和调用2)中,它找到第一个块的开始和结束行。然后再调用两次(分别调用3和4)以获得下一个开始和结束行号。 但是 在第三个调用(调用3)上,它没有返回值,而是转到addFormulaTestDataLoop。但是,当我单步执行代码时,搜索例程End Function中包含正确的行号。

gFindInColumn

该函数的调用方式如下:

Public Function gFindInColumn(search As Variant, columnNum As Double, Optional rowNum As Double) As Double

If rowNum = 0 Then
    rowNum = 2
End If
gFindInColumn = Columns(columnNum).Find(What:=search, _
                                After:=Cells(rowNum, columnNum), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False, _
                                SearchFormat:=False).row
'MsgBox "value = " & gFindInColumn, vbExclamation, "Finished"
End Function

它在dlgSysteEPatchTempDateTime.Show temp = dlgSysteEPatchTempDateTime.txtBPreBaseLineEndTemp.Value sStr = dlgSysteEPatchTempDateTime.txtBPreBaselineStartDate.Value & " " & dlgSysteEPatchTempDateTime.txtBPreBaselineStartTime.Value eStr = dlgSysteEPatchTempDateTime.txtBPreBaselineEndDate.Value & " " & dlgSysteEPatchTempDateTime.txtBPreBaselineEndTime.Value sRow(1, 1) = gFindInColumn(sStr, 3) eRow(1, 1) = gFindInColumn(eStr, 3) Call addFormulaTestDataLoop(temp, sRow(1, 1), eRow(1, 1)) 'Dur temp = dlgSysteEPatchTempDateTime.txtBDurBaseLineEndTemp.Value sStr = dlgSysteEPatchTempDateTime.txtBDurBaselineStartDate.Value & " " & dlgSysteEPatchTempDateTime.txtBDurBaselineStartTime.Value eStr = dlgSysteEPatchTempDateTime.txtBDurBaselineEndtDate.Value & " " & dlgSysteEPatchTempDateTime.txtBDurBaselineEndTime.Value qtyDur = dlgSysteEPatchTempDateTime.txtDurQty.Value sRow(2, 2) = gFindInColumn(sStr, 3) eRow(2, 2) = gFindInColumn(eStr, 3) Range("A" & sRow(2, 2)).Value = "DURING" Call addFormulaTestDataLoop(temp, sRow(2, 2), eRow(2, 2)) 处失败,但是可以在前两个通话中使用吗?

1 个答案:

答案 0 :(得分:2)

似乎您只想查找在特定行号之后出现的匹配项:在这种情况下,您需要确保Find()不会循环返回...

Public Function gFindInColumn(search As Variant, rngCol As Range, _
                              Optional rowNum As Long = 2) As Long
    Dim f As Range
    Set f = rngCol.Find(What:=search, After:=rngCol.Cells(rowNum), _
                        LookIn:=xlValues, LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False, _
                        SearchFormat:=False)

    If Not f Is Nothing Then
        'only return non-zero if the found cell is *after* rowNum
        gFindInColumn = IIf(f.Row > rowNum, f.Row, 0)
    Else
        gFindInColumn = 0
    End If

End Function

还可以将“搜索列”切换到Range对象,因此对于“查找”在哪张纸上运行没有任何疑问。

请注意,您也可以直接在参数列表中处理可选参数的默认值。