Excel Macro To Cut Rows And Paste Into Another Worksheet

那年仲夏 提交于 2019-12-11 17:37:32

问题


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:


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


来源:https://stackoverflow.com/questions/24252205/excel-macro-to-cut-rows-and-paste-into-another-worksheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!