【问题标题】:vlookup to look for value in second columnvlookup 在第二列中查找值
【发布时间】:2013-09-29 17:18:57
【问题描述】:

我有以下 2 张牌桌(赔率和赌注)。

表1

表 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 函数)中处理这种情况。有没有办法解决这个问题?

【问题讨论】:

    标签: vba indexing vlookup


    【解决方案1】:

    通过删除第一列来计算 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)
    

    【讨论】:

    • 我试过这段代码,但它导致了同样的错误。我已经编辑了我的帖子以包含工作表的图像供您参考。 .Cell(1,2) 是否指向第 2 列?
    • 是的。如果顶部的黄色条是也包含在范围内的合并单元格,它将不起作用。 OddsTable5 必须只包含表格,有或没有标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多