我想知道这段代码有什么问题。我的目标是将数据从sheet2复制到sheet1。代码正在运行,但是复制到shee1的记录被替换为row1中的数据,它不会附加。我希望将数据复制到每一行的下面。
Set ws = Sheets("Sheet2")
With ws
Set rngsh1 = .UsedRange
End With
For Each cel1 In rngsh1
With Sheets("Sheet1")
Set rng = .Rows(1).Find(cel1, LookIn:=xlValues, LookAt:=xlWhole)
If Not rng Is Nothing Then
cel1.Offset(, 1).Copy .Cells(rng.Row + 1, rng.Column)
End If
End With
Next
答案 0 :(得分:1)
这会将Sheet2中的值附加到Sheet1上的新行:
For Each cel1 In Worksheets("Sheet2").UsedRange
With Worksheets("Sheet1")
Set rng = .UsedRange.Rows(1).Find(cel1, LookIn:=xlValues, LookAt:=xlWhole)
If Not rng Is Nothing Then
.Cells(.UsedRange.Rows.Count + 1, rng.Column) = cel1.Offset(, 1)
End If
End With
Next
但您的初始代码仅搜索Sheet1上的第1行。我想搜索所有值:
For Each cel1 In Worksheets("Sheet2").UsedRange
With Worksheets("Sheet1")
Set rng = .UsedRange.Find(cel1, LookIn:=xlValues, LookAt:=xlWhole)
If Not rng Is Nothing Then
.Cells(.UsedRange.Rows.Count + 1, rng.Column) = cel1.Offset(, 1)
End If
End With
Next