关联不同列表中的值

时间:2015-02-08 18:15:20

标签: excel

我有一个包含3300个姓名和地址的列表。唯一的名称只有300.为了对它们进行地理编码,我搜索了唯一的地址,获取了每个地址的坐标。

现在我在两张MS excel的表格中有这两个列表,我想将每个名字与他的地址坐标联系起来。

i.imgur.com/hmaLgCD.png

2 个答案:

答案 0 :(得分:0)

我建议将LatitudeLongitude列添加到您的蓝色表中,而不是创建绿色列。

使用以下公式:

=INDEX(Latitude_orange_column, MATCH(address1_blue_cell, Address_orange_column, 0))
=INDEX(Longitude_orange_column, MATCH(address1_blue_cell, Address_orange_column, 0))

答案 1 :(得分:0)

这是一个VBA版本,它允许您灵活地(1)自定义工作表名称和起始数据的位置,以及(2)将“结果”放入任何起始单元位置的任何工作表中。

本质上,代码将数据放入数组中,对其进行处理并将结果放入第三个数组中。然后很容易将“结果”数组传输到指定工作表上的指定范围。

注意:在数据副本上运行代码,因为它会从名称数据中删除重复项。

Option Explicit
Sub AssocLocs()
Dim wsName As Worksheet, wsLocn As Worksheet, wsRes As Worksheet
Dim nmeRng As Range, locnRng As Range
Dim nmeStRow As Long, nmeEndRow As Long, nmeCol As Long
Dim locnStRow As Long, locnEndRow As Long, locnCol As Long
Dim resStRow As Long, resCol As Long
Dim N As Long, L As Long, e As Long
Dim nmeArr(), locnArr(), resArr()

'Setup======================================
    'worksheets
    Set wsName = Sheets("Sheet1")   'name data
    Set wsLocn = Sheets("Sheet2")   'location data
    Set wsRes = Sheets("Sheet1")    'results
    'original data (x2) cell start positions
    nmeStRow = 2    'header row
    nmeCol = 2
    locnStRow = 2   'header row
    locnCol = 2
    'result destination cell start position
    resStRow = 2
    resCol = 6
'===========================================

    'unique name data into array
    With wsName
        nmeEndRow = .Cells(Rows.Count, nmeCol).End(xlUp).Row
        Set nmeRng = .Range(.Cells(nmeStRow + 1, nmeCol), .Cells(nmeEndRow, nmeCol + 1))
        nmeRng.Sort Key1:=.Range("B3"), Order1:=xlAscending, Header:=xlYes
        nmeRng.RemoveDuplicates Columns:=1 ', Header:=xlYes
        nmeEndRow = .Cells(Rows.Count, nmeCol).End(xlUp).Row
        Set nmeRng = .Range(.Cells(nmeStRow + 1, nmeCol), .Cells(nmeEndRow, nmeCol + 1))
        nmeArr() = nmeRng.Value
    End With

    'sorted location data into array
    With wsLocn
        locnEndRow = .Cells(Rows.Count, locnCol).End(xlUp).Row
        Set locnRng = .Range(.Cells(locnStRow, locnCol), .Cells(locnEndRow, locnCol + 2))
        locnRng.Sort Key1:=.Range("B3"), Order1:=xlAscending, Header:=xlYes
        locnArr() = locnRng.Value
    End With

e = -1

    For N = LBound(nmeArr()) To UBound(nmeArr())
        For L = LBound(locnArr()) To UBound(locnArr())
            If nmeArr(N, 2) = locnArr(L, 1) Then
                e = e + 1
                ReDim Preserve resArr(4, e)
                resArr(0, e) = nmeArr(N, 1)
                resArr(1, e) = nmeArr(N, 2)
                resArr(2, e) = locnArr(L, 2)
                resArr(3, e) = locnArr(L, 3)
                Exit For
            End If
        Next L
    Next N

    With wsRes
        .Range(.Cells(resStRow, resCol), .Cells(resStRow + UBound(resArr, 2), resCol + 4)) = Application.Transpose(resArr)
    End With

End Sub

最后,this article是一个非常好的读取,证明使用循环来迭代数组。