Code reduction exercise

Dryver14

Well-known Member
Joined
Mar 22, 2010
Messages
2,396
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?

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
 
Expanding on Peter's comments, with large numbers of ranges, sorting the data is often best. This groups all the rows with X together so there is only 1 single range to move/copy/delete - this is fast, whereas if there are large numbers (say 5,000 discontinuous ranges in a list of 60,000 rows) it can be really slow. Resort to original order if required.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Thanks, this is why I looked at the autofilter option.

Thanks for the comments, I can only improve through them.

In my real life I rarely come across data sets larger than a couple of hundred rows.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top