使用另一个电子表格replaceExcel数据

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

“索引”电子表格的布局如下(作为例子)。

AB 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列replace为“索引”电子表格的相应索引,可能使用VBA。

结果会是这样的;

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

我已经看到了一些使用switch语句的静态方法,但我真的希望能够从另一个电子表格中导入数据。 我试图不诉诸复制粘贴和“全部replace”。

谢谢!

尝试这个:

 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 

注意:

就我个人而言,我将原样保留原始数据表,并使用查看数据表和索引表的演示文稿表格,并将其呈现给您。 那么你不一定需要代码,如果你仍然想要使用代码,那么代码会更简单。 您将需要添加代码来closures屏幕更新和使用error handling等。

ja72解决scheme的替代scheme:

 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 

它也更快地悄然自信;)

用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 

这将是最快的。

我不相信VBA是解决这个问题的最好方法。 一个简单的VLOOKUP可以完成你需要做的事情。

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

会给你你正在寻找的结果。