从一个Excel工作簿更新2列到其他工作簿

我有两张工作表A和B

书A有3列

Elements (duck, elephant,deer,monkey), Weight(20,70,18,25), Size(10,30,10,6) 

分别。

书B也有相同的列Elements (elephant,deer), Weight (80,28) and size(40,20)分别。

我需要更新Book A中书籍B的动物列的重量和大小。

鹿的体重:28和大小:20书B.然后它应该更新鹿的重量和体积在书A,而不是体重:18和大小:10。

我看到VLOOKUP函数和一些相关的问题,但无法find它。 请帮助我。 提前致谢..!!!

你可以试试这个:

首先打开书B离开书A封闭右键单击工作表名称,并select查看代码复制并粘贴在编辑的地方按绿色播放button或F5

*****在运行之前,请不要忘记保存文件的备份副本,以防数据损坏*****

 Sub updateBook() Dim loop1, loop2 As Long Dim rows1, rows2 As Integer Dim Path As String Path = "Path to Book A" 'edit within the quotes the path to Book A file eg C:\users\name\BookA.xlsm Dim openWb As Workbook Set openWb = Workbooks.Open(Path) Dim openWs As Worksheet Set openWs = openWb.Sheets("Sheet Name in Book A") 'edit within the quotes the sheet name of Book A Dim currentWb As Workbook Set currentWb = ThisWorkbook Dim currentWs As Worksheet Set currentWs = currentWb.Sheets("Sheet Name in Book B") 'edit within the quotes the sheet name of Book B rows1 = currentWs.UsedRange.Rows.Count rows2 = openWs.UsedRange.Rows.Count For loop1 = 1 To rows1 Step 1 For loop2 = 1 To rows2 Step 1 If openWs.Cells(loop2, 1).Value = currentWs.Cells(loop1, 1).Value Then 'check if names match between books openWs.Cells(loop2, 2).Value = currentWs.Cells(loop1, 2).Value 'replaces the old value in 2nd column with new value from Book A openWs.Cells(loop2, 3).Value = currentWs.Cells(loop1, 3).Value 'replaces the old value in 3rd column with new value from Book A End If Next loop2 Next loop1 Application.DisplayAlerts = False openWb.Close (True) 'saves and closes book B End Sub