细胞匹配,但有些细胞有空间

时间:2014-11-28 11:22:09

标签: regex excel excel-vba vba

我有以下问题。

enter image description here

1 - 我试图查看B列中的单元格与A列中的单元格之间是否存在匹配

2 - 如果匹配,我们将B列的单元格复制到C列的第一个单元格中。

一个例子:1001Pharmacies中的B7A5:A7中匹配。因此我将其复制到C2

除此之外,我的另一个问题是我的一些单元格之间有空格,就像B9中的空格一样,因此无法与A11:A13中的单元格进行正确匹配。

如果你有任何见解,我会很高兴。

更新:我已经尝试过这个作为起点

Function andyLookup(strInput As String, rngTable As Range, defaultvalue As String) As String
    Dim strArray() As String
    Dim i As Integer
    Dim strOut As String
    Dim temp1 As Variant

    strArray = Split(strInput, " ")
    For i = 0 To UBound(strArray)

        temp1 = Application.VLookup(strArray(i), rngTable, 2, 0)
        'if not found, apply default value
        If IsError(temp1) Then
        strOut = strOut & defaultvalue & " "
        Else
        strOut = strOut & temp1 & " "
        End If

    Next

    'remove trailing comma
    strOut = Left(strOut, Len(strOut) - 1)
    andyLookup = strOut

End Function

1 个答案:

答案 0 :(得分:0)

你的意思是

=IFERROR(VLOOKUP(SUBSTITUTE(B2," ",""),A:B,2,FALSE),"")

或者,如果您特别想使用VBA

Function andyLookup(strInput As String, rngTable As Range, defaultvalue As String) As String

Dim temp1 As Variant

     temp1 = Application.VLookup(Replace(strInput," ",""), rngTable, 2, 0)

     andLookup = IIf(IsError(temp1), defaultvalue, temp1)

End Function