搜索范围内的文本,如果匹配,则返回匹配值

时间:2015-04-03 10:27:54

标签: vba excel-vba excel

我有街道名称和地址列表。现在我想查找地址列中是否有与街道名称匹配的值,然后返回街道名称。

enter image description here

2 个答案:

答案 0 :(得分:0)

假设地址在A1:

For AddressRow = FirstRow To LastRow
    address = Cells(AddressRow,1)
    For StreetRow = FirstRow To LastRow
        If "*" & Cells(StreetRow,3) & "*" Like address Then MsgBox Cells(StreetRow,3)
    Next
Next

我希望您可以定义FirstRow,LastRow并知道您希望输出现在转到消息框的位置。

答案 1 :(得分:0)

假设地址列为A Street_name列为C。这段代码不是最优雅但有效。

Private Sub DoTheJob()
    Dim intRowStreetName As Integer
    Dim intRowAdress As Integer
    intRowStreetName = 1
    intRowAdress = 1
    strPartOfStreetName = "nguyen"
    With Worksheets("Sheet1")
        Do While .Cells(intRowStreetName, 3).Value <> Empty
            intRowAdress = 1
            If InStr(1, .Cells(intRowStreetName, 3).Value, strPartOfStreetName, vbTextCompare) > 0 Then
                Do While .Cells(intRowAdress, 1).Value <> Empty
                    If InStr(1, .Cells(intRowAdress, 1).Value, .Cells(intRowStreetName, 3).Value, vbTextCompare) > 0 Then
                       .Cells(intRowAdress, 2).Value = .Cells(intRowStreetName, 3).Value
                    End If
                intRowAdress = intRowAdress + 1
                Loop
            End If
            intRowStreetName = intRowStreetName + 1
        Loop
    End With
End Sub