简体   繁体   中英

VBA Lookup with Dynamic Range

snap of first data range

I'm looking for some help with running a VBA lookup Function. I have setup two search functions designed to find the start and end of the data and set that as the range of the vlookup. The part I'm having trouble with appears to be setting the range correctly. I have the integer values of the rows and the columns should remain standard. The data will be between Columns B and I.

The currently shown code appears to rather than set the boundries of the code in the range area, return the value of those cells which of courser results in an error. Thanks In advance :)

Does anybody know I would go about setting the range/fixing the vlookup ? the current error: unable to get the vlookup property of the worksheet function class

PotatoePriceEuro.value and lengthinputtext.value are textbox inputs on a userform. Truecheck is a global variable from earlier on inside the module, it contains the keyword searched for in the first two search functions.

The goal of the program is to search a sheet and find the first and last occurance of a particular string as given by a textbox in the userform (the string in truecheck) and then set that as the range for the vlookup. the vlookup is then passed a numeric term from another textbox on the userform (lengthinputtext.value) which then searches column C for that number and returns the value of the cell to its left. Note that the keyword for setting the range is in Column B and the length to be search within the range will be in column 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

The final solution ended up with me giving up on using a vlookup and instead resorting to using a Find function and then offsetting it to retrieve the information, looking something like this (still not all cleaned up properly). I'm not sure why it took me so long to give up on the vlookup and why it wasn't working.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM