簡體   English   中英

具有動態范圍的VBA查找

[英]VBA Lookup with Dynamic Range

第一個數據范圍的快照

我正在尋找有關運行VBA查找功能的幫助。 我設置了兩個搜索功能,旨在查找數據的開始和結束並將其設置為vlookup的范圍。 我遇到問題的部分似乎是正確設置了范圍。 我有行的整數值,列應保持標准。 數據將在B列和I列之間。

當前顯示的代碼似乎不是在邊界區域中設置代碼的邊界,而是返回那些單元格的值,這當然會導致錯誤。 提前致謝 :)

有人知道我會去設置范圍/修復vlookup嗎? 當前錯誤:無法獲取工作表函數類的vlookup屬性

PotatoePriceEuro.value和lengthinputtext.value是用戶窗體上的文本框輸入。 Truecheck是模塊內部較早版本的全局變量,它包含在前兩個搜索功能中搜索的關鍵字。

該程序的目標是搜索工作表並找到特定字符串的第一個和最后一個出現(如用戶窗體中的文本框所指定)(truecheck中的字符串),然后將其設置為vlookup的范圍。 然后,從用戶窗體上的另一個文本框(lengthinputtext.value)向vlookup傳遞一個數字項,然后在C列中搜索該數字並將單元格的值返回到其左側。 請注意,用於設置范圍的關鍵字在B列中,並且要在該范圍內搜索的長度將在C列中

Private Sub optionselect()
    Dim LastLocation As Range
    Dim FirstLocation As Range
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim SearchVal As String
    Dim returnval As Integer

    Set FirstLocation = Range("B:B").Find(truecheck, LookIn:=xlValues, _ 
    LookAt:=xlWhole, SearchOrder:=xlByRows)

    Set LastLocation = Range("B:B").Find(truecheck, LookIn:=xlValues, _ 
    LookAt:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlPrevious)

    FirstRow = FirstLocation.Row
    LastRow = LastLocation.Row

    PotatoPriceEuro.Value = Application.WorksheetFunction.VLookup(LengthInputText.Value, _ 
    Range(Cells(FirstRow, 3), Cells(LastRow, 9)), 2, False)

End Sub

最終的解決方案是我放棄使用vlookup,而是求助於使用Find函數,然后偏移它以檢索信息,看起來像這樣(仍然不能完全清除)。 我不確定為什么要花這么長時間放棄vlookup,以及為什么它不起作用。

Private Sub optionselect()
    Dim LastLocation As Range
    Dim FirstLocation As Range
    Dim FirstRow As Long
    Dim LastRow As Long
    Dim SearchVal As String
    Dim returnval As Integer
    Dim lastlocationoff As Variant
    Dim rng As Range
    Dim resultfind As Range
    Dim Columoff As Integer

    lengthvlook = LengthInputText.Value

    Set FirstLocation = Worksheets("Bucket  Elevator"_ 
    ).Range("B:B").Find(truecheck, LookIn:=xlValues, lookat:=xlWhole _ 
    , searchorder:=xlByRows)
    Set LastLocation = Worksheets("Bucket Elevator" _ 
    ).Range("B:B").Find(truecheck, LookIn:=xlValues, lookat:=xlWhole _ , 
    searchorder:=xlByRows, searchdirection:=xlPrevious)

    FirstRow = FirstLocation.Row
    LastRow = LastLocation.Row

    Set resultfind = Worksheets("Bucket Elevator").Range("C" & FirstRow & _ 
    ":C" & LastRow).Find(LengthInputText.Value, LookIn:=xlValues, _ 
    lookat:=xlWhole, searchorder:=xlByRows)

    PektusPriceEuro = Worksheets("bucket elevator").Cells(resultfind.Row, 4)

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM