Hi, I was playing with a very small piece of code, essentially remove rows with X to new sheet and delete from Data sheet.
I am trying to see how far I can reduce the code, all three work and it is only a small learning exercise for me.
Is this as good as it gets or do you have some advice?
I am trying to see how far I can reduce the code, all three work and it is only a small learning exercise for me.
Is this as good as it gets or do you have some advice?
Code:
Sub MoveData()
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim LastRow As Long
Dim NextRow As Long
Dim i As Long
Set WS1 = Worksheets("Data")
Set WS2 = Worksheets("Report")
LastRow = WS1.Cells(Rows.Count, 1).End(xlUp).Row
NextRow = 2
WS1.Range("A1:E1").Copy WS2.Range("A1")
WS1.Select
For i = LastRow To 2 Step -1
If Cells(i, 2).Value = "Z" Then
Cells(i, 1).Resize(1, 5).Copy WS2.Cells(NextRow, 1)
NextRow = NextRow + 1
Cells(i, 1).Resize(1, 5).EntireRow.Delete
End If
Next i
End Sub
Sub SmartMoveData()
Worksheets("Data").Range("A1").CurrentRegion.AutoFilter field:=2, Criteria1:="Z"
ActiveSheet.Cells(2, 1).CurrentRegion.Copy Worksheets("Report").Range("A1")
Application.DisplayAlerts = False
ActiveSheet.Range("A2", Range("A1").End(xlDown).End(xlToRight)).Delete
Application.DisplayAlerts = True
Worksheets("Data").Range("A1").CurrentRegion.AutoFilter
End Sub
Sub SmartMoveData2()
Application.DisplayAlerts = False
With Worksheets("Data")
.Range("A1").CurrentRegion.AutoFilter field:=2, Criteria1:="Z"
.Cells(2, 1).CurrentRegion.Copy Worksheets("Report").Range("A1")
.Range("A2", Range("A1").End(xlDown).End(xlToRight)).Delete
.Range("A1").CurrentRegion.AutoFilter
End With
Application.DisplayAlerts = True
End Sub