Excel宏切割行并粘贴到另一个工作表

时间:2014-06-16 21:06:38

标签: excel excel-vba excel-2010 vba

每当第I列等于LS时,我试图获取一个宏来剪切并粘贴工作表ASR到工作表LS的某些行。

Sub MoveLS()
    Dim i As Variant
    Dim endrow As Integer

    endrow = Sheets("ASR").Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To endrow
    If Cells(i, "I").Value = "LS" Then
       Cells(i, "I").EntireRow.Cut Destination:=Sheets("LS").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
    Next

End Sub

过去8小时我一直在盯着这段代码的不同变化,并且无法弄清楚什么不起作用。任何提示都表示赞赏!

1 个答案:

答案 0 :(得分:1)

这是因为你没有宣布你的床单。请尝试以下代码:

Sub MoveLS()
    Dim i As Variant
    Dim endrow As Integer
    Dim ASR As Worksheet, LS As Worksheet

    Set ASR = ActiveWorkbook.Sheets("ASR")
    Set LS = ActiveWorkbook.Sheets("LS")

    endrow = ASR.Range("A" & ASR.Rows.Count).End(xlUp).Row

    For i = 2 To endrow
        If ASR.Cells(i, "I").Value = "LS" Then
           ASR.Cells(i, "I").EntireRow.Cut Destination:=LS.Range("A" & LS.Rows.Count).End(xlUp).Offset(1)
        End If
    Next
End Sub