VBA:根据Vlookup结果将值移动到另一个工作表



所以我有一个代码在Vlookup上执行循环,其目的是基于vlookup,它将特定单元格移动到另一个特定的单元格,但在另一个工作表中。代码运行,但由于某种原因不会移动数据。我在 If 注释中尝试了另一种方法,并使用复制目标执行范围,但我开始收到对象未识别错误。希望有人能看到我可能错过了为什么这些值没有被移动。

Dim i, j, lastG, lastD As Long
Dim sht, ws As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
.CutCopyMode = False
End With
' find last row
lastG = Sheets("Log").Cells(Rows.Count, "B").End(xlUp).Row
lastD = Sheets("Slide Layout").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Row
Set sht = Sheets("Log")
Set ws = Sheets("Slide Layout")
' loop over values in "Log"
For i = 2 To lastG
currVal = ws.Cells(Rows.Count, "J").End(xlUp)
lookupval = Sheets("Log").Cells(i, "B") ' value to find
If lookupval = currVal Then
sht.Cells(i, "F") = ws.Cells(lastD)
End If
Next i
On Error Resume Next
With Application
.ScreenUpdating = True
.EnableEvents = True
.CutCopyMode = True
End With
End Sub

不确定我是否完全遵循...但看起来你应该有这个

嘶嘶。单元格(i, "F"( = 当前值

更新,想通了!我刚刚替换了

sht.Cells(i, "F") = ws.Cells(lastD)

sht.Cells(i, "F").Copy Destination:=ws.Cells(lastD, "B")

我很感激你的帮助。

最新更新