【问题标题】:Vlookup using VBA Function使用 VBA 函数进行 Vlookup
【发布时间】:2020-02-04 16:11:43
【问题描述】:

我有两个电子表格:

电子表格用户:

ID                     NAME
jg1988            Junior Gomez
rs1772            Rose Mills

电子表格数据和链接:

USERS
Jeff Connor,Rose Mills,Junior Gomez,Michael smith

我需要一个 Vlookup 函数来查看用户并获取匹配项的 ID。

在这种情况下:

rs1772,jg1988

【问题讨论】:

    标签: excel vba vlookup


    【解决方案1】:

    使用用户定义的函数是可能的。在模块中定位并用作 =A_Lookup(A6,"Users","B","A") 其中 A6 是名称字符串,“Users”是查找表,“B”是名称列,“ A" 的 ID。

    Function A_Lookup(names As String, sht As String, colName As String, colID As String) As String
    
        Dim rngName As Range, rngID As Range
        With ThisWorkbook.Sheets(sht)
            Set rngName = .Range(colName & ":" & colName)
            Set rngID = .Range(colID & ":" & colID)
        End With
    
        Dim ar As Variant, s As String, n As Integer, i As Integer
        ar = Split(names, ",")
        s = ""
        On Error Resume Next
        For n = 0 To UBound(ar)
            i = 0
            If Len(ar(n)) > 0 Then
                i = WorksheetFunction.Match(CStr(ar(n)), rngName, 0)
                If i > 0 Then
                    If s <> "" Then s = s & ","
                    s = s & rngID.Cells(i, 1)
                End If
            End If
        Next
        On Error GoTo 0
        A_Lookup = s
    
    End Function
    

    【讨论】:

    • =A_Lookup(B848,"Sheet1",J6:J1955,K6:K1955) 这是我正在尝试的,并且正在给我#Value!
    • 仅使用列名,例如 =A_Lookup(B848,"Sheet1","J","K")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    相关资源
    最近更新 更多