VLookup multiple columns

前端 未结 4 1861
刺人心
刺人心 2021-01-07 09:55

I am using VLookup function which looks up multiple values which are present in the column. This works very well but just takes a lot of time as I have 100,000 rows in the E

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-07 10:59

    This is about 20-30x faster than a simple loop (tested over a column of 20k values, with 3 matches to the value being searched).

    'rng: a single-column range to search for matches
    'val: the value to match on
    'col: offset from match in rng to concatenate values from (similar
    '       to the matching VLOOKUP argument)
    Function MultiLookup(rng As Range, val As String, col As Long)
    
        Dim i As Long, v, s
        Dim r As Long
    
        r = rng.Cells.Count
        v = Application.Match(val, rng, 0)
        s = ""
        Do While Not IsError(v)
            s = s & IIf(s <> "", ",", "") & rng.Cells(v).Offset(0, col - 1).Value
            r = r - v
            Set rng = rng.Offset(v, 0).Resize(r, 1)
            v = Application.Match(val, rng, 0)
        Loop
        MultiLookup = s
    
    End Function
    

提交回复
热议问题