Copy Pasting a lot of data using ActiveCell Offset

dejamls

New Member
Joined
Nov 22, 2015
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi
Below is some code I have. There's actually a lot of copying and pasting going on between workbooks (more than I show below).
I would like to speed up the macro and was wondering is there better faster way to copy paste a number of cells on a worksheet that have been identified with a ActiveCell.Offset to another workbook sheet.
Cheers in advance

Code:
Sub CopyPaste()
 
Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    
    Dim wsDestA As Worksheet 'Destination workbook + sheet    
    Set wsDestA = Workbooks("SRS Bulk Sample Lodgement.xlsm").Worksheets("Atmospheric") 'Destination workbook + sheet
    
    'Sample Date
    ActiveCell.Offset(0, 1).Copy
    wsDestA.Range("B56535").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'Sample ID
    ActiveCell.Offset(0, 2).Copy
    wsDestA.Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'First Name
    ActiveCell.Offset(0, 3).Copy
    wsDestA.Range("L65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'Last Name
    ActiveCell.Offset(0, 4).Copy
    wsDestA.Range("K65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'DoB
    ActiveCell.Offset(0, 18).Copy
    wsDestA.Range("M1000000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'Gender
    ActiveCell.Offset(0, 19).Copy
    wsDestA.Range("N65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True

End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
There shouldn't be any need to "Copy & Paste" - just set one range equal to the other....
Code:
Dim wsDestA As Worksheet 'Destination workbook + sheet
    Set wsDestA = Workbooks("SRS Bulk Sample Lodgement.xlsm").Worksheets("Atmospheric") 'Destination workbook + sheet
    
    'Sample Date
    
    wsDestA.Range("B56535").End(xlUp).Offset(1, 0).Value = ActiveCell.Offset(0, 1)

    'Sample ID
    
    wsDestA.Range("A65536").End(xlUp).Offset(1, 0).Value = ActiveCell.Offset(0, 2)
Etc etc...
 
Upvote 0
You could also use a "With" statement to shorten the code even further:
Code:
    With wsDestA
        'Sample Date
        .Range("B56535").End(xlUp).Offset(1, 0).Value = ActiveCell.Offset(0, 1)
        'Sample ID
        .Range("A65536").End(xlUp).Offset(1, 0).Value = ActiveCell.Offset(0, 2)
    End With
 
Upvote 0
Pleasure! Has it improved your project?

Out of interest, in your "Atmospheric" worksheet, does all the data always finish on the same row? - i.e. when the code fires, is it going along the same row, and pushing the data into the various cells of that next available row? I'm assuming that it does.
If this IS the case, then we could speed things up AND further reduce your code - quite significantly:
Code:
Dim wsDestA As Worksheet 'Destination workbook + sheet
Dim nxt_rw As Long

Set wsDestA = Workbooks("SRS Bulk Sample Lodgement.xlsm").Worksheets("Atmospheric") 'Destination workbook + sheet
    
    With wsDestA
         nxt_rw = .Range("A65536").End(xlUp).Row + 1
        'Sample Date
        .Range("B" & nxt_rw).Value = ActiveCell.Offset(0, 1)
        'Sample ID
        .Range("A" & nxt_rw).Value = ActiveCell.Offset(0, 2)
    End With
... as long as all of the next set of data goes onto the same row, we can just find the next blank row once, and refer to the ranges using this row number; we've then saved your code having to search for the last-used row on the "Atmospheric" sheet every time it posts another piece of data accross.
Should improve things dramatically!
 
Upvote 0
Pleasure! Has it improved your project?

Out of interest, in your "Atmospheric" worksheet, does all the data always finish on the same row? - i.e. when the code fires, is it going along the same row, and pushing the data into the various cells of that next available row? I'm assuming that it does.
If this IS the case, then we could speed things up AND further reduce your code - quite significantly:
Code:
Dim wsDestA As Worksheet 'Destination workbook + sheet
Dim nxt_rw As Long

Set wsDestA = Workbooks("SRS Bulk Sample Lodgement.xlsm").Worksheets("Atmospheric") 'Destination workbook + sheet
    
    With wsDestA
         nxt_rw = .Range("A65536").End(xlUp).Row + 1
        'Sample Date
        .Range("B" & nxt_rw).Value = ActiveCell.Offset(0, 1)
        'Sample ID
        .Range("A" & nxt_rw).Value = ActiveCell.Offset(0, 2)
    End With
... as long as all of the next set of data goes onto the same row, we can just find the next blank row once, and refer to the ranges using this row number; we've then saved your code having to search for the last-used row on the "Atmospheric" sheet every time it posts another piece of data accross.
Should improve things dramatically!

Hi sykes, yes the previous code you gave me helped improve the speed quite a lot. I will try your next gem tomorrow and let you know how it goes. Many thanks for your help.
 
Upvote 0
Pleasure. Thanks for the feedback.
 
Upvote 0
Hi sykes

Tested your latest recommendation. Your improved code worked really good and executes much faster now. Really appreciate your assistance.

Cheers
 
Upvote 0

Forum statistics

Threads
1,215,880
Messages
6,127,519
Members
449,385
Latest member
KMGLarson

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