Lookup function for matching ID's and then populating an additional value - Vlookup

我与影子孤独终老i 提交于 2019-12-11 04:36:21

问题


I have a data set:

ID Name Amount BID        BID 2
1  Jack 100     1          
1  John 200     *Blank*    
2  Tony 300     2          

How can I lookup the ID and where the ID is matching look at the BID and find the relating ID value where the ID value has been populated for the same ID. I want the value to appear in BID 2 which is in column 5.

IE John should have a BID of 1 as his ID is 1 like Jacks ID.

I have tried

 =vlookup(A2,A2:C5,3,FALSE)

回答1:


I honestly don't know a way to do this through formula, but a VBA solution would look like this:

Sub FindTheValue()

Dim sht As Worksheet, lastrow As Long, i As Long, j As Long

Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "C").End(xlUp).Row

For i = 1 To lastrow
    If Range("Z" & i).Value = "" Then
        For j = 1 To lastrow
            If Range("C" & i).Value = Range("C" & j).Value And Range("Z" & j).Value <> "" Then
                Range("AA" & i).Value = Range("Z" & j).Value
                Exit For
            End If
        Next j
    End If
Next i

End Sub



来源:https://stackoverflow.com/questions/49015763/lookup-function-for-matching-ids-and-then-populating-an-additional-value-vloo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!