vlookup在第二列中查找值

时间:2013-09-29 08:28:56

标签: vba indexing vlookup

我有以下2个表(赔率和投注)。

表1

Table1

表2:

Table 2

我想将表2中的TransId与表1中的TransId进行比较并检索一些值并将其粘贴到表2中的Price列中。 我的VBA代码中有一个VLOOKUP函数来完成这项工作。然而,它遍历第一列(oddsId),因此提取错误的价格(希望这是预期的,因为Vlookup总是寻找最左边的列,如果我没有错)。

但我想比较两种TransId来获取价格信息。

价格栏使用公式:

=getprice(BetsTable[[#This Row],[TransId]],BetsTable[[#This Row],[Option]])   

以下是GetPrice的代码示例:

Function GetPrice(transId, opt)
Dim bettype As String

opt = UCase(opt)

bettype = Application.WorksheetFunction.VLookup(transId, Range("OddsTable5"), 3, False)

If (bettype = "FT.HDP" Or bettype = "HT.HDP") Then
    If (opt = "H") Then
        GetPrice = Application.WorksheetFunction.VLookup(transId, Range("OddsTable5"), 14, False)
    ElseIf (opt = "A") Then
        GetPrice = Application.WorksheetFunction.VLookup(transId, Range("OddsTable5"), 15, False)
    Else
        GetPrice = "Error"
    End If

我想在我的VBA代码(getPrice函数)中处理这种情况。有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

通过删除第一列来计算要使用的VLookup的范围。

dim odds_range as range
with Range("OddsTable5")
  set odds_range = Range( .Cells(1,2), .Cells(.Rows.Count, .Columns.Count) )
end with

...

GetPrice = Application.WorksheetFunction.VLookup(transId, odds_range, 14, False)
相关问题