[英]Embedding vlookup in an excel function
I am trying to correlate data that is pulled from a huge database. 我正在尝试关联从巨大数据库中提取的数据。 I have no control over the database, so renaming fields is not an option.
我无法控制数据库,因此无法重命名字段。 I had to separate data from different database tables onto different sheets.
我不得不将数据从不同的数据库表分离到不同的工作表上。 I'm trying to write a function in excel to return specific vlookup values based on a single criteria.
我正在尝试在excel中编写一个函数,以基于单个条件返回特定的vlookup值。 The "itemnum" values are in column A and the "binnum" values are in column K. I need to be able to use the function for a sheet that has 19,000 unique values for "itemnum".
“ itemnum”值在A列中,“ binnum”值在K列中。我需要能够对具有19,000个“ itemnum”唯一值的工作表使用该函数。 The problem is, I don't know the syntax for using vlookup in VBA.
问题是,我不知道在VBA中使用vlookup的语法。 Here is an example of what I'm trying to do:
这是我要执行的操作的一个示例:
Function orders(itemnum, binnum)
If binnum = "ibp" Then
orders = IF(ISNA(VLOOKUP(AllLocations!A2,OpenIBP!A:B,1,FALSE)), 0, VLOOKUP(AllLocations!A2,OpenIBP!A:B,2,FALSE))
Else
orders = =IF(ISNA(VLOOKUP(AllLocations!A2,OpenIST!A:B,1,FALSE)), 0, VLOOKUP(AllLocations!A2,OpenIST!A:B,2,FALSE))
End If
End Function
Can anyone help me rewrite this so it works? 谁能帮我改写它,使其起作用? I'm in way over my head.
我在头上。 Thanks.
谢谢。
You can use it like this: 您可以像这样使用它:
V=application.vlookup("A",range("A1:B5"),2,0)
If iserror(v) then
Msgbox "Fail"
Else
Msgbox v
End if
V is a variant, I am looking for "A" column A, and return value in B V是一个变体,我正在寻找“ A”列A,并在B中返回值
To bridge the gap between the code sample that you had, and the answer that user3423985 gave, here is a variation (assuming that I understand what you were trying to do): 为了弥合您所拥有的代码示例与user3423985给出的答案之间的差距,这是一个变体(假设我了解您要执行的操作):
Option Compare Text
Function orders(itemnum, binnum)
' look up itemnum in either sheet OpenIBP or sheet OpenIST
' depending on the value of binnum
Dim sheetName As String
If binnum = "ibp" Then
sheetName = "OpenIBP"
Else
sheetName = "OpenIST"
End If
Dim sh As Worksheet
Set sh = ActiveWorkbook.Sheets(sheetName)
' now we can look up something in the sheet:
Dim value
' Look up the value itemnum
' in column A of sheet sh
' return the corresponding value in column 2
' return exact match only
value = Application.VLookup(itemnum, sh.[A:B], 2, False)
' check for error in lookup
If IsError(value) Then
MsgBox "Item " & itemnum & " not found!"
Else
orders = value
End If
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.