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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Just noticed the last 2 do not work if the data sheet is not selected before use....Another line of code!
 
Upvote 0
Avoid selecting things - it is almost never required.

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")
    
   With WS1
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    NextRow = 2
    
    .Range("A1:E1").Copy WS2.Range("A1")

        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 With
End Sub

I'd also suggest turning off screenupdating and calculations while running the code.
 
Upvote 0
Cheers Rory,

Are you saying the loop is better than the other 2 as no selection required, I get you point on the screen updating, I am only working with 60 lines of data for this exercise so didn't feel it was warranted
 
Last edited:
Upvote 0
No, the same principle would apply there, I'm just lazy.

Code:
Sub SmartMoveData()


With Worksheets("Data")
   .Range("A1").CurrentRegion.AutoFilter field:=2, Criteria1:="Z"
   .Cells(2, 1).CurrentRegion.Copy Worksheets("Report").Range("A1")
   Application.DisplayAlerts = False
   .Range("A2", .Range("A1").End(xlDown).End(xlToRight)).Delete
   Application.DisplayAlerts = True
   .Range("A1").CurrentRegion.AutoFilter
End With

End Sub
 
Upvote 0
I am trying to see how far I can reduce the code ..
Depends what you mean by "reduce the code". Do you mean
a) Have as few lines of code as possible?
b) Have the code do its job in the shortest possible time?

... do you have some advice?
I think often people focus on a) at the expense of b) but there are a number of considerations including

- The size (no. of rows) of the data. If the data is not very big, speed probably will not be an issue, but if it is very large then speed may be very important.
- What proportion of the rows and in how many distinct areas will likely need removal to the other sheet.
- Are there any formulas on the sheet or just constant values.
- If there are formulas, how many and what type.
- Who might need to maintain the code in the future and what their vba skills are like.
- etc

In the end, each case really needs to be judged on its merits in deciding the "best" code to use.
 
Upvote 0
Thanks Peter,

I take your points on board...early stages, as I develop more complex procedures I can see each item needs to be addressed, as a side from this is there anywhere with large sheets of data that I can set myself projects on?
 
Upvote 0
.. as a side from this is there anywhere with large sheets of data that I can set myself projects on?
I'm usually just trying to answer forum questions & mostly generate the data myself based on any sample data or information in the question.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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