-1

修复:检查user3964075的评论

下面我的简单代码需要帮助:它基本上是 vlookup 的不同版本,您还可以在其中指定要查找的行。

asda(fval, rng, fcol, rcol)

fval是用户正在寻找的

rng是范围

fcol是 vlookup 默认的,设置为 1,现在用户可以选择使用哪一列作为搜索的基础

rcol是找到匹配项时返回的列

请参见下面的代码:

Function asda(fval As Range, rng As Range, fCol As Integer, rCol As Integer)

    Dim row As Variant

    For Each row In rng.Rows
        If fval.Value = rng.Columns(fCol).Rows(row).Value Then
            result = rng.Columns(rCol).Rows(row).Value
            GoTo found
        End If
    Next

found:
    asda = result

End Function

问题:它不起作用,我不知道为什么。由于我想使用其他人的代码,我想从我的开始并修复它。

为将来阅读此内容的任何人修复了代码:

Function asda(fval As Range, rng As Range, fCol As Integer, rCol As Integer)
Dim row As Variant
Dim rowc As Integer

rowc = 1
For Each row In rng.Rows
    If fval.Value = rng.Cells(rowc, fCol).Value Then
        result = rng.Cells(rowc, rCol).Value
        Exit For
    End If
    rowc = rowc + 1
Next

asda= result

结束功能

4

1 回答 1

0

查看第一个解释的评论。

并且当您row用作变量时,您无法从Range 类中调用Row 属性,因此我将其更改为 use 。RowVRowV.row

编译已经说过的内容:

Function asda(fval As Range, rng As Range, fCol As Integer, rCol As Integer)

    Dim RowV As Range

    For Each RowV In rng.Rows
        If fval.Value <> rng.Cells(RowV.row, fCol).Value Then
        Else
            asda = rng.Columns(rCol).Rows(RowV.row).Value
            'Exit For
            Exit Function
        End If
    Next RowV

    asda = "Value not found"

End Function
于 2015-05-29T13:07:10.127 回答