[英]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.