当列F值在OTD分析中不存在时,我想将列从工作表W2W复制并粘贴到工作表OTD分析。
此代码复制列F:Au而不是A:AU。
Sub Transfer()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Sheets("W2W").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim rng As Range
Dim foundVal As Range
For Each rng In Sheets("W2W").Range("F2:F" & LastRow)
Set foundVal = Sheets("OTD Analysis").Range("F:F").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
If foundVal Is Nothing Then
rng.Columns("A:AU").Copy
Sheets("OTD Analysis").Activate
b = Sheets("OTD Analysis").Cells(Rows.Count,1).End(xlUp).Row
Sheets("OTD Analysis").Cells(b + 1, 1).Select
ActiveSheet.Paste
End If
Next rng
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
2条答案
按热度按时间rlcwz9us1#
您希望
Columns("A:AU")
引用整行。wkyowqbh2#
传送新条目
rng
是单元格F2
,那么rng.Columns("A:AU")
指的是范围F2:AZ2
,rng.EntireRow
指的是范围A2:XFD2
,rng.EntireRow.Columns("A:AU")
是指范围A2:AU2
。