Copy a Range
- This illustrates how you can rewrite the code and each time make it more efficient.
- There is also a duration (on my machine) of each code for a thousand rows supplied, which is showing that only the last two improvements are acceptable.
- How will you show hundreds of numbers on the status bar in a split second anyway? Just forget about using the status bar.
Option Explicit
Sub CopyInitial() ' 48s
Const sfRow As Long = 2 ' Source First Row
Const dfRow As Long = 1 ' Destination First Row
Dim slRow As Long ' Source Last Row
slRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Dim dcRow As Long ' Destination Current Row
dcRow = dfRow
Dim r As Long ' Source Current Row
For r = sfRow To slRow
Sheet1.Select
Range(Cells(r, "A"), Cells(r, "B")).Select
Selection.Copy
Sheet2.Select
Cells(dcRow, "A").Select
ActiveSheet.Paste
Application.StatusBar = r & " of " & slRow
dcRow = dcRow + 1
Next r
End Sub
Sub CopyNoStatusBar() ' 40s
Const sfRow As Long = 2 ' Source First Row
Const dfRow As Long = 1 ' Destination First Row
Dim slRow As Long ' Source Last Row
slRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Dim dcRow As Long ' Destination Current Row
dcRow = dfRow
Dim r As Long ' Source Current Row
For r = sfRow To slRow
Sheet1.Select
Range(Cells(r, "A"), Cells(r, "B")).Select
Selection.Copy
Sheet2.Select
Cells(dcRow, "A").Select
ActiveSheet.Paste
dcRow = dcRow + 1
Next r
End Sub
Sub CopyNoSelect() ' 19s
Const sfRow As Long = 2 ' Source First Row
Const dfRow As Long = 1 ' Destination First Row
Dim slRow As Long ' Source Last Row
slRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Dim dcRow As Long ' Destination Current Row
dcRow = dfRow
Dim r As Long ' Source Current Row
For r = sfRow To slRow
Sheet1.Range(Sheet1.Cells(r, "A"), Sheet1.Cells(r, "B")).Copy _
Sheet2.Cells(dcRow, "A")
dcRow = dcRow + 1
Next r
End Sub
Sub CopyNoScreenUpdating() ' 13s
Const sfRow As Long = 2 ' Source First Row
Const dfRow As Long = 1 ' Destination First Row
Dim slRow As Long ' Source Last Row
slRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Dim dcRow As Long ' Destination Current Row
dcRow = dfRow
Dim r As Long ' Source Current Row
Application.ScreenUpdating = False
For r = sfRow To slRow
Sheet1.Range(Sheet1.Cells(r, "A"), Sheet1.Cells(r, "B")).Copy _
Sheet2.Cells(dcRow, "A")
dcRow = dcRow + 1
Next r
Application.ScreenUpdating = True
End Sub
Sub CopyNoLoop() ' 0.05s
Const sfRow As Long = 2 ' Source First Row
Const dfRow As Long = 1 ' Destination First Row
Dim slRow As Long ' Source Last Row
slRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Sheet1.Range(Sheet1.Cells(sfRow, "A"), Sheet1.Cells(slRow, "B")).Copy _
Sheet2.Cells(dfRow, "A")
End Sub
Sub CopyByAssignment() ' 0.017s (but values only)
Const sfRow As Long = 2 ' Source First Row
Const dfRow As Long = 1 ' Destination First Row
Dim slRow As Long ' Source Last Row
slRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
Dim srg As Range
Set srg = Sheet1.Range(Sheet1.Cells(sfRow, "A"), Sheet1.Cells(slRow, "B"))
Dim drg As Range
Set drg = Sheet2.Cells(dfRow, "A") _
.Resize(srg.Rows.Count, srg.Columns.Count)
drg.Value = srg.Value
End Sub