Excel:使用工作表中的值作为索引,以便在不同工作表中列出并replace第一个工作表中的值

我有一个XL文件与一些数据被操纵。 我想我将需要使用VB脚本来做到这一点 – 但也许有一个公式更简单的方法。 同样,有人可能会指出实现以下目标的方法吗?

在工作表1中有一列数字值(ID)。我想使用每个ID作为索引来查找工作表2中的列表。工作表2有两列第一列是索引,第二列是文本string例如

1苹果

2橙色

3梨

我想要的是用工作表2中查找的文本stringreplace工作表1中的ID列!

那是所有…请帮助!

那里不是一个艰难的情况。 这里有一些解决scheme…

用VBA:

我知道你说过你对VB有一点新意,所以我试着解释每一行。 另外,代码是自由的,所以如果我在那里留下一个错误,请原谅我。

 Sub replaceData() dim i as integer, j as integer 'These are just some variables we'll use later. dim sheetOne as worksheet, sheetTwo as worksheet, myWb as workbook dim myData as string, myId as string set myWB = excel.activeworkbook 'These three lines set your workbook/sheet variables. set sheetOne = myWB.worksheets("Old Data") set sheetTwo = myWB.worksheets("New Data") for i = 1 to sheetTwo.usedrange.rows.count 'This loops through the rows on your second sheet. myId = sheetTwo.cells(i,1).value 'This assigns the value for your id and the data on your second sheet. myData = sheetTwo.cells(i,2).value for j = 1 to sheetOne.usedrange.rows.count 'This loops through the rows on your first sheet. if sheetOne.cells(j,1).value = myId then 'This checks each row for a matching id value. sheetOne.cells(j,1).value = myData 'This replaces that id with the data we got from the second sheet. end if next j next i end sub 

用Excel公式:

  1. 将下面的公式放置在第一个工作表的单元格C1(带有要replace的ID的工作表)中。 **请注意 ,您必须将第二张工作表的名称replace为“InsertSheetTwoNameHere”部分(不要删除这些单引号)。 另外请注意,您将需要用表2中最后一次使用的行号replace“1000”。

    = VLOOKUP(A1, 'InsertSheetTwoNameHere' $ A $ 1:$ B $ 1000,2,FALSE)

  2. 接下来只需拖动单元格上的句柄,使它自己复制(不pipe它叫做什么),一直到你的范围的末尾。

  3. 接下来,复制这些单元格,然后使用“ 仅限值”设置将它们粘贴到ID上。

希望这有助于和好运。