使用其他电子表格替换Excel数据

时间:2012-02-14 20:49:12

标签: excel vba excel-vba

我有两个excel电子表格,一个包含各种人的数据,而另一个则是一个索引。

'指数'电子表格如下(作为示例);

      A       B
1 [person:][pID: ]
2 [Mark   ][1    ]
3 [James  ][2    ]
4 [John   ][3    ]
5 [David  ][4    ]
n [ ...   ][n    ]

'数据'电子表格如下(作为示例);

      A
1 [pID:   ][ ...
2 [David  ][ ...
3 [David  ][ ...
4 [David  ][ ...
5 [James  ][ ...
6 [Mark   ][ ...
7 [David  ][ ...
n [ ...   ][ ...

我想要做的是更换数据'中的A列。电子表格,以及“索引”的相应索引。电子表格,可能使用VBA。

结果将是这样的;

      A
1 [pID:   ][ ...
2 [4      ][ ...
3 [4      ][ ...
4 [4      ][ ...
5 [2      ][ ...
6 [1      ][ ...
7 [4      ][ ...
n [ ...   ][ ...

我见过一些使用switch语句的静态方法,但我真的希望能够从另一个电子表格中导入数据。 我试图不去复制粘贴和“全部替换”。

谢谢!

3 个答案:

答案 0 :(得分:4)

试试这个:

Sub HTH()

    With Sheets("Data")
        .Columns(2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        .Cells(1, "B").Value = .Cells(1, "A").Value
        With .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Offset(, 1)
            .Formula = "=VLOOKUP(A2,Index!A:B,2,FALSE)"
            .Value = .Value
        End With
        .Columns(1).Delete
    End With

End Sub

说明:

Sub HTH()

    With Sheets("Data")
        '// Insert a new column with same format
        .Columns(2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        '// Copy the header
        .Cells(1, "B").Value = .Cells(1, "A").Value
        '//  Work with the used range of column A 
        With .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Offset(, 1)
            '// Use a formula to replace the names with corresponding values
            .Formula = "=VLOOKUP(A2,Index!A:B,2,FALSE)"
            '// Replace formula with value
            .Value = .Value
        End With
        '//  Delete the old column
        .Columns(1).Delete
    End With

End Sub

注意:

就个人而言,我会保留原始数据表,并使用一个演示文稿表查找数据表和索引表,并按照您的需要提供。那么你不一定需要代码,如果你仍然想要使用代码,那么代码将更加简单。您需要添加代码以关闭屏幕更新并使用错误处理等。

ja72解决方案的替代方案:

Sub HTH()
    Dim vNames As Variant, vResult As Variant
    Dim lLoop As Long

    vNames = Sheets("Index").UsedRange.Columns(1).Resize(, 2).Value2
    vResult = Sheets("Data").UsedRange.Columns(1).Value2

    With CreateObject("Scripting.Dictionary")
        For lLoop = 2 To UBound(vNames, 1)
            .Add vNames(lLoop, 1), vNames(lLoop, 2)
        Next lLoop

        For lLoop = 2 To UBound(vResult, 1)
            vResult(lLoop, 1) = .Item(vResult(lLoop, 1))
        Next lLoop

        Sheets("Data").Range("A1").Resize(UBound(vResult, 1), 1).Value = vResult
    End With

End Sub

悄悄地确信它也更快;)

答案 1 :(得分:1)

尝试使用VBA:

Public Sub FindPID()
    Dim i As Integer, N As Integer
    Dim r_index As Range
    ' Find start of index
    Set r_index = Sheets("Index").Range("A2")
    ' Count rows in index
    N = r_index.Worksheet.Range(r_index, r_index.End(xlDown)).Rows.Count
    ' Expand range with all values
    Set r_index = r_index.Resize(N, 2)
    Dim vals As Variant
    ' Move values into array
    vals = r_index.Value2
    ' Create a lookup collection
    Dim lup As New Collection
    For i = 1 To N
        ' Add each pID to collection with name as key
        lup.Add vals(i, 2), vals(i, 1)
    Next i

    Dim r_data As Range, M As Integer
    ' Find start of data
    Set r_data = Sheets("Data").Range("A2")
    ' Count rows in data
    M = r_data.Worksheet.Range(r_data, r_data.End(xlDown)).Rows.Count
    ' Expand range with all values
    Set r_data = r_data.Resize(M, 1)
    ' Move values into array
    vals = r_data.Value2
    For i = 1 To M
        ' Replace first column with lookup pID
        vals(i, 1) = lup(vals(i, 1))
    Next i
    ' Assign values from array back to cells.
    r_data.Value2 = vals
End Sub

这将是最快的。

答案 2 :(得分:1)

我不相信VBA是这个问题的最佳解决方案。一个简单的VLOOKUP可以完全满足您的需求。

=VLOOKUP(Data!$A1, Index!$A$2:$A$5, 1, FALSE)

会为您提供您正在寻找的结果。