2013-09-29 118 views
0

我有以下2个表格(赔率&投注)。vlookup在第二列中查找值

表1

Table1

表2:

Table 2

我想从表2 TRANSID从表1比较TRANSID和检索一些值,并粘贴在价格列相同在表2中。 我在我的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函数)。有什么办法可以解决这个问题吗?

回答

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) 
+0

我试过这段代码,但是结果是一样的错误。我编辑了我的帖子以包含图片以供参考。 .Cell(1,2)是否指向第2列? – Shan

+0

是的。如果顶部的黄色条是包含在该范围内的合并单元格,则不起作用。 “OddsTable5”只能包含表格,有或没有标题。 – GSerg