Find and FindNext for Excel VBA

后端 未结 5 1854
谎友^
谎友^ 2020-12-03 16:12

I\'ve been stuck trying to figure out what to do with this, but basically I want a way to print out the value in column B given a specific value that matches column A. So fo

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 17:18

    Here is an implementation of the suggestion I made in my comment under your question.

    Function RowBeforeLast(ByVal What As Variant) As Long
    
    Dim Fnd As Range
    
    Set Fnd = Range("E:E").Find(What:=What, After:=Range("E1"), _
                                LookAt:=xlWhole, _
                                Searchdirection:=xlPrevious)
    If Not Fnd Is Nothing Then
        Set Fnd = Range("E:E").Find(What:=What, After:=Fnd, _
                                    LookAt:=xlWhole, _
                                    Searchdirection:=xlPrevious)
        If Not Fnd Is Nothing Then RowBeforeLast = Fnd.Row
    End If
    

    End Function

    It's designed as a UDF so that you can call it from the worksheet with a worksheet function like =RowBeforeLast(E5). You can also call it with code like

    Private Sub TestGet()
        RowBeforeLast "GR 3"
    End Sub
    

    Either way it will return the row number in which the search criterium was found for the second time from the bottom of the column. If there is only one or no occurrance the function will return zero.

提交回复
热议问题