这并不难;)请看一个好的起点
我正在为您提供与您在链接中提供的电子表格一起使用的代码。它将"Sheet1"
根据"Sheet2"
注释中的列表填充: 确保您的工作表被称为 Sheet1 和 Sheet2 或相应地修改代码
Sub Main()
Dim ws1 As Worksheet, ws2 As Worksheet ' sheet variables declaration
Set ws1 = Sheets("Sheet1"): Set ws2 = Sheets("Sheet2") ' binding sheets to variables
Dim rng1 As Range, rng2 As Range ' range variables
Dim i As Long, j As Long, k As Long ' iterators
' for each cell in column F in sheet2
For i = 2 To ws2.Range("F" & Rows.Count).End(xlUp).Row
Set rng2 = ws2.Range("F" & i) ' binding cells from column F (sheet2) to rng2 variable
' for each cell in column B on sheet1
For j = 2 To ws1.Range("B" & Rows.Count).End(xlUp).Row
Set rng1 = ws1.Range("B" & j) ' binding cells from column B (sheet1) to rng1 variable
' comparing both words ( names )
If StrComp(rng2, rng1, 1) = 0 Then
For Each Column In Sheet1
For k = 3 To ws1.Cells(1, Columns.Count).End(xlToLeft).Column
' if the name of column matches the offset or rng2 (name)
If StrComp(rng2.Offset(0, 1), Cells(1, k), 1) = 0 Then
' copy/paste the amount of fruits from sheet2 to corresponding cells in sheet1
Cells(rng1.Row, k) = rng2.Offset(0, 2)
End If
Next k ' next column
End If
Set rng1 = Nothing
Next j ' next row in sheet1
Set rng2 = Nothing
Next i ' next row in sheet2
结束子
结果