问题
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