Excel Macro To Cut Rows And Paste Into Another Wor

2019-08-31 17:39发布

I am trying to get a macro to cut and paste certain rows from sheet ASR to sheet LS, whenever column I is equal to 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

I have been staring at different variations of this code on and off for the past 8 hours and cannot figure out what isn't working. Any tips are appreciated!

1条回答
祖国的老花朵
2楼-- · 2019-08-31 18:17

It is because you haven't declared your sheets. Try the following code:

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
查看更多
登录 后发表回答