Excel VBA:将单元格中的定界字符串与值列进行比较

时间:2018-10-24 22:20:10

标签: excel vba excel-vba

我在B列中有一个(以分号分隔的)基因列表,我想从该列表中创建 ,这是在A列中找到的基因列表。

| Keep             | List                       |   | Result      |
|------------------|----------------------------|---|-------------|
| AASS;SESN1;SEPT5 | AASS                       |   | AASS        |
|                  | ARMC2;SESN1;ARMC2AS1;SEPT5 |   | SESN1;SEPT5 |
|                  |                            |   |             |

我从一个代码开始,但是它似乎只适用于某些基因列表,但不是全部。

例如,将单元格B2和B3中的列表正确提取到列C中,但是单元格B4以另外7个词结尾(但是第二次运行VBA脚本会得到正确的数字和组成),而B5结果在D5的一个奇怪的输出“ 4; 5; 0; 2; 3; 1; SNORD1161”中。

这是我到目前为止所拥有的代码,它被修改自:https://www.mrexcel.com/forum/excel-questions/654920-match-comma-delimited-values-cell-against-individual-values-column.html

任何帮助将不胜感激!谢谢!

Sub matchups2()

    Dim regex_leading As New VBScript_RegExp_55.RegExp
    Dim regex_middle As New VBScript_RegExp_55.RegExp
    Dim regex_trailing As New VBScript_RegExp_55.RegExp

    Set d = CreateObject("scripting.dictionary")
    For Each gene In Range("A2", Cells(Rows.Count, "A").End(3)).Value
        d(gene) = 1
    Next gene
    Stop

    For Each genelist In Range("B2", Cells(Rows.Count, "B").End(3))
        c = genelist.Value
        k = genelist.Row

        For Each q In Split(c, ";")
            If d(q) <> 1 Then
                c = Replace(c, q, ";")
            End If
        Next q

        regex_leading.Pattern = "^;{1,}"
        With regex_middle
            .Pattern = ";{1,}"
            .Global = True
        End With
        regex_trailing.Pattern = ";{1,}$"

        c = regex_leading.Replace(c, "")
        c = regex_middle.Replace(c, ";")
        c = regex_trailing.Replace(c, "")

        Cells(k, "D").Value = c
    Next genelist

End Sub

1 个答案:

答案 0 :(得分:0)

我认为这应该对您有用。

Sub GenesDict()

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    'add A genes to dictionary
    Dim i As Long
    For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
        Dim temp As Variant
        temp = Split(Cells(i, "A").Value2, ";")

        Dim j As Long
        For j = LBound(temp) To UBound(temp)
            dict.Add Trim(temp(j)), "text"
        Next j
    Next i

    'clear D
    Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row).ClearContents

    'transfer from B to D only genes in A
    For i = 2 To Cells(Rows.Count, "B").End(xlUp).Row
        temp = Split(Cells(i, "B").Value2, ";")

        For j = LBound(temp) To UBound(temp)
            If dict.exists(Trim(temp(j))) Then
                Cells(i, "D").Value2 = Cells(i, "D").Value2 & Trim(temp(j)) & ";"
            End If
        Next j

        'remove trailing ";"
        If Right(Cells(i, "D").Value2, 1) = ";" Then
            Cells(i, "D").Value2 = Left(Cells(i, "D").Value2, Len(Cells(i, "D").Value2) - 1)
        End If

    Next i

End Sub