简体   繁体   English

具有动态范围的VBA查找

[英]VBA Lookup with Dynamic Range

snap of first data range 第一个数据范围的快照

I'm looking for some help with running a VBA lookup Function. 我正在寻找有关运行VBA查找功能的帮助。 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. 我设置了两个搜索功能,旨在查找数据的开始和结束并将其设置为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. 数据将在B列和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 ? 有人知道我会去设置范围/修复vlookup吗? the current error: unable to get the vlookup property of the worksheet function class 当前错误:无法获取工作表函数类的vlookup属性

PotatoePriceEuro.value and lengthinputtext.value are textbox inputs on a userform. PotatoePriceEuro.value和lengthinputtext.value是用户窗体上的文本框输入。 Truecheck is a global variable from earlier on inside the module, it contains the keyword searched for in the first two search functions. Truecheck是模块内部较早版本的全局变量,它包含在前两个搜索功能中搜索的关键字。

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. 该程序的目标是搜索工作表并找到特定字符串的第一个和最后一个出现(如用户窗体中的文本框所指定)(truecheck中的字符串),然后将其设置为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. 然后,从用户窗体上的另一个文本框(lengthinputtext.value)向vlookup传递一个数字项,然后在C列中搜索该数字并将单元格的值返回到其左侧。 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 请注意,用于设置范围的关键字在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

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). 最终的解决方案是我放弃使用vlookup,而是求助于使用Find函数,然后偏移它以检索信息,看起来像这样(仍然不能完全清除)。 I'm not sure why it took me so long to give up on the vlookup and why it wasn't working. 我不确定为什么要花这么长时间放弃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