[英]LOOKUP formula to find last value in VBA
I want to convert a lookup formula into Worksheet function but it prompts Type mismatch
error.我想将查找公式转换为工作表函数,但它提示
Type mismatch
错误。
This formula working fine.这个公式工作正常。
=LOOKUP(2,1/(SalesDB!I6:I3005="FLOUR"),SalesDB!K6:K3005)
This prompts Type mismatch
error这提示
Type mismatch
error
ActiveSheet.Range("Q9").Value = Application.WorksheetFunction.Lookup(2, 1 / (Sheets("SalesDB").Range("$I$6:$I$3005") = "FLOUR"), Sheets("SalesDB").Range("$K$6:$K$3005"))
I want to lookup last value from a column so Vlookup
formula does not work on it.我想从一列中查找最后一个值,所以
Vlookup
公式Vlookup
不起作用。 How can I solve the error?我该如何解决错误?
The 'trick' you are using with the LOOKUP
function won't easily translate to VBA. 您在
LOOKUP
函数中使用的“技巧”不会轻易转换为 VBA。 You can use a simple Find
method with the search order reversed (parameter SearchDirection:=xlPrevious
) which will find the last match and then use the Offset
function to go across the columns to retrieve the value you want.您可以使用反向搜索顺序的简单
Find
方法(参数SearchDirection:=xlPrevious
),它将找到最后一个匹配项,然后使用Offset
函数SearchDirection:=xlPrevious
列以检索您想要的值。
Option Explicit
Sub Test()
ActiveSheet.Range("Q9").Value = LastVlookup("FLOUR", Sheets("SalesDB").Range("$I$6:$K$3005"), 3)
End Sub
Function LastVlookup(varLookup As Variant, rngData As Range, lngOffset As Long) As Variant
LastVlookup = rngData.Find( _
What:=varLookup, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchDirection:=xlPrevious).Offset(0, lngOffset - 1).Value
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.