VBA中有excle1sheet1中A1:B1单元格,遍历并复制excle2sheet1中与excle1sheet1的A1:B1有相同的C列的值到excel1sheet1中的相应位置上
时间: 2023-06-01 21:08:07 浏览: 130
好的,这个问题涉及到Excel VBA编程,属于技术问题,我可以为您提供以下代码:
Sub CopyData()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastRow1 As Long, lastRow2 As Long, i As Long, j As Long
Set ws1 = ThisWorkbook.Sheets("excle1sheet1")
Set ws2 = ThisWorkbook.Sheets("excle2sheet1")
lastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
lastRow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow1
For j = 2 To lastRow2
If ws1.Cells(i, 1).Value = ws2.Cells(j, 3).Value And ws1.Cells(i, 2).Value = ws2.Cells(j, 4).Value Then
ws2.Range("C" & j & ":D" & j).Copy ws1.Range("A" & i & ":B" & i)
Exit For
End If
Next j
Next i
MsgBox "Data copied successfully!", vbInformation, "Copy Data"
End Sub
这段代码可以将Excel2中与Excel1中A1:B1单元格有相同C列的值复制到Excel1中相应位置上。 请注意:在使用此代码之前,请将excle1sheet1和excle2sheet1替换为您的实际工作簿和工作表名称。
阅读全文