Why is the wrong row being copied and pasted

2019-08-17 14:24发布

New to VBA

I am trying to loop through a table and copy the row containing a name. When I run the code, the output is the row below the one I want.

Sub tblcopypast()

Dim Name As String
Dim tbl As ListObject
Dim i As Integer
Dim lastrow As Integer

Set tbl = ActiveSheet.ListObjects("Table1")
Name = ActiveSheet.Range("E1").Value
lastrow = tbl.ListRows.Count

For i = 1 To lastrow
    If tbl.Range(i, 1) = Name Then
    tbl.ListRows(i).Range.Copy
    ActiveSheet.Range("E5").PasteSpecial xlPasteValuesAndNumberFormats
    Application.CutCopyMode = False
    End If
Next

End Sub

标签: excel vba
0条回答
登录 后发表回答