Perform a FIND, within vba, from the bottom of a range up

╄→尐↘猪︶ㄣ 提交于 2019-12-02 06:32:40

问题


Good Afternoon,

Is it possible for Find to start from the bottom of a range and work up? What I would like is my code to first find a record number that will be located on a master list. Once it finds the record number I want it to assign that deals name, an offset of the record number, to a variable and then search up the master list for the first deal with that same name.

Currently, I have code that finds the record number, assigns the deal name to a variable and then loop's up each cell until it finds a match. Although this way works, the loop processing time is significantly slower than the find processing time and I am searching for the fastest solution.

If reverse find is not a possibility, would a vlookup work? Possibly by, creating a range beginning 1 row above the record number to the top and have vlookup find the last occurance?

Any help is appreciated.

PendingBRow = ThisWorkbook.Sheets("PendingLog").Range("A65000").End(xlUp).Row
MasterBRow = ThisWorkbook.Sheets("MasterLog").Range("A65000").End(xlUp).Row

For D = 2 To PendingBRow
    With ThisWorkbook.Sheets("PendingLog").Range("A" & D)
        PendingRecNum = .Value
        PendingDealName = .offset(0, 3).Value
        PDLenght = Len(PendingDealName) - 4
        PendingDealName = Left(PendingDealName, PDLenght)
        PendingDealName = UCase(PendingDealName)
        PendingDealName = Trim(PendingDealName)
    End With

    With ThisWorkbook.Sheets("MasterLog").Range("B2:B" & MasterBRow)
        Set c = .Find(PendingRecNum, LookIn:=xlValues)
        If Not c Is Nothing Then
            firstRow = c.Row - 1

            O = 1
            Do Until firstRow = O
                LastWorkedBy = ThisWorkbook.Sheets("MasterLog").Range("E" & firstRow).offset(0, 20)
                MasterRecNum = ThisWorkbook.Sheets("MasterLog").Range("E" & firstRow).offset(0, -3).Value
                dealName = ThisWorkbook.Sheets("MasterLog").Range("E" & firstRow).Value
                dealName = Left(dealName, 10)
                dealName = UCase(dealName)
                dealName = Trim(dealName)

                If PendingDealName = dealName Then
                    MasterLastWorkedBy = LastWorkedBy
                    ThisWorkbook.Sheets("PendingLog").Range("A" & D).offset(0, 19).Value = MasterLastWorkedBy

                    firstRow = O
                Else
                    firstRow = firstRow - 1
                End If

            Loop

        End If
    End With

Next D

回答1:


This will FIND() from the bottom:

Sub FindFromTheBottom()
    Set a = Range("A:A").Find("Test", after:=Cells(1, 1), searchdirection:=xlPrevious)
    MsgBox a.Address(0, 0)
End Sub



回答2:


the after cell specified has to be within the search range; if you remove after:=, then active cell is taken as the after cell.



来源:https://stackoverflow.com/questions/22464631/perform-a-find-within-vba-from-the-bottom-of-a-range-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!