Vlookup多个用分号分隔的值

时间:2018-10-22 10:29:03

标签: excel excel-formula cell vlookup matching

我需要帮助对齐两个列中的相同值,在两个列中,一个单元格中用分号分隔的代码不止一个。

我有这样一栏:

UMLS CODE
C0443147
C0441748;C4020899
C4025900
C0085606;C3544092;C4020898

我需要将以下数据与上一列匹配。

UMLS CODE  TYPE    MEDDRA CODE         DEFINITION
C0443147    LT;PT   10014275;10014407   EEG;Electroencephalogram
C4020899    LT;PT   10014544;10014430   EMG;Electromyogram
C3544092    OL;LT   10014828;10014449   Electronystagmography
C0013854    PT;LT   10014455;10014359   Electro-oculogram

因此,与UMLS CODES列匹配的结果必须是这样的:

UMLS CODE                  UMLS CODE  TYPE    MEDDRA CODE         DEFINITION
C0443147                   C0443147   LT;PT   10014275;10014407   EEG;Electroencephalogram
C0441748;C4020899          C4020899   LT;PT   10014544;10014430   EMG;Electromyogram
C4025900                   -------    -----   -----------------   -------------------
C0085606;C3544092;C4020898 C3544092   OL;LT   10014828;10014449   Electronystagmography

我在excel上尝试了以下公式,但当查找值具有多个用分号分隔的值时,该方法不起作用。

  

= VLOOKUP($ A1; $ A $ 13819:$ D $ 63379; COLUMN(A:A); 0)

其中$ A1是UMLS CODE,而$ A $ 13819:$ D $ 63379是与UMLS CODE匹配的所有数据。

来自UMLS代码的图片: enter image description here

来自UMLS数据的图片: enter image description here

期望的结果,但也可以在用UMLS代码用分号分隔的多个值单元格中使用: enter image description here

2 个答案:

答案 0 :(得分:2)

假设您的列表中有最多UMLS码(我假设为3)

第8-12行中的表是一个中间步骤,只是为了简化操作,如果您愿意,可以将其放在最终函数中。

= TRIM(MID(SUBSTITUTE($ A5;“;” ;; REPT(“”; LEN($ A5)));(C $ 8)* LEN($ A5)+1; LEN($ A5))))

enter image description here

= IFERROR(VLOOKUP($ A12; $ C $ 2:$ F $ 5; 4; FALSE); IFERROR(VLOOKUP($ B12; $ C $ 2:$ F $ 5; 4; FALSE); VLOOKUP($ C12; $ C $ 2:$ F $ 5; 4; FALSE)))

enter image description here

列A16只是= A2等

答案 1 :(得分:1)

@ S.Ram

如图所示将数据导入Sheet1中:

enter image description here

如图所示将数据导入Sheet2中:

enter image description here

然后尝试:

Option Explicit
Option Explicit

Sub test()

Dim LastRow1 As Long
Dim LastRow2 As Long
Dim i As Long
Dim j As Long
Dim Word As String
Dim Word1 As String
Dim Word2 As String
Dim SpecialChr As Long
Dim Position As Long
Dim Position2 As Long

LastRow1 = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
LastRow2 = Sheet2.Range("B" & Rows.Count).End(xlUp).Row

 With Sheet1

    For i = 2 To LastRow1

        SpecialChr = (Len(.Range("A" & i).Value) - Len(Replace(.Range("A" & i).Value, ";", ""))) / Len(";")

        If SpecialChr = 0 Then
            Word = .Range("A" & i).Value
            With Sheet2
                For j = 2 To LastRow2
                    If .Range("B" & j).Value = Word Then
                        .Range("B" & j).Offset(0, -1).Value = Word
                    End If
                Next j
            End With
        ElseIf SpecialChr = 1 Then
            Position = InStr(1, .Range("A" & i).Value, ";")
            Word = Left(.Range("A" & i).Value, Position - 1)
            Word1 = Right(.Range("A" & i).Value, Position - 1)
            With Sheet2
                For j = 2 To LastRow2
                    If .Range("B" & j).Value = Word Then
                        .Range("B" & j).Offset(0, -1).Value = Word
                    ElseIf .Range("B" & j).Value = Word1 Then
                        .Range("B" & j).Offset(0, -1).Value = Word1
                    End If
                Next j
            End With
        ElseIf SpecialChr = 2 Then
            Position = InStr(1, .Range("A" & i).Value, ";")
            Position2 = InStr(Position + 1, .Range("A" & i).Value, ";")
            Word = Left(.Range("A" & i).Value, Position - 1)
            Word1 = Mid(.Range("A" & i).Value, Position + 1, Len(.Range("A" & i).Value) - Position2)
            Word2 = Right(.Range("A" & i).Value, Position - 1)
            With Sheet2
                For j = 2 To LastRow2
                    If .Range("B" & j).Value = Word Then
                        .Range("B" & j).Offset(0, -1).Value = Word
                    ElseIf .Range("B" & j).Value = Word1 Then
                        .Range("B" & j).Offset(0, -1).Value = Word1
                    ElseIf .Range("B" & j).Value = Word2 Then
                        .Range("B" & j).Offset(0, -1).Value = Word2
                    End If
                Next j
            End With
        End If
    Next i
End With

End Sub

最后,结果将导入到Sheet2黄色区域。