VBA Copy Paste Help

Mitchx

New Member
Joined
Oct 20, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi there, I need your assistance. I wrote some code and it copy pastes succesfully, however it goes line by line according to the if statement and takes a long long time.

Basically what I want is to copy the entire column of the source sheet (with entire column I mean the data that falls within the if statement) So for example all the data of July 2022 and nothing else.
I want to copy paste the columns B, D , E , H, V, X and AE to the destination sheet (which is a table) to the columns A, B , C , D, H, I , and N respectively.

The data must be appended to the last row of the destination table. And since the source sheet is also dynamic in range I need to find the last row too.

Below is the code I have for now. Some lines are being commented so don't pay attention to those. Any guidance is appreciated, many thanks!

VBA Code:
Sub Example()

Dim Last_Col As Long
Dim FoundDate As Range
Dim Last_Row As Long
Set wsDest = ThisWorkbook.Worksheets("ATP combi")
Set wsSource = ThisWorkbook.Worksheets("ATP-waarde bij inzet")
Dim lr As Long, r As Long
Dim tbl As ListObject
'Quit looping at the encounter of a blank cell with a forloop and finding the last row that contains data



Set tbl = wsDest.ListObjects("Tabel3")

Last_Row = wsDest.ListObjects("Tabel3").Range.Columns(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

MsgBox Last_Row

'    For i = Last_Row To 1 Step -1
'    If IsEmpty(wsDest.Cells(i, 1)) Then
'        Last_Row = i
'    Exit For
'    End If
'Next

With wsSource

  lr = .Cells(Rows.Count, "A").End(xlUp).Row
  For r = lr To 2 Step -1
    If IsDate(.Range("A" & r).value) Then
        If Month(.Range("A" & r).value) = Month(Now()) - 1 And Year(.Range("A" & r).value) = Year(Now()) Then
        Last_Row = Last_Row + 1
    'wsDest.Range("A" & Last_Row & ":B" & Last_Row).value = wsSource.Range("A" & r & ":H" & r).value
    wsDest.Range("A" & Last_Row).value = wsSource.Range("B" & r).value
    wsDest.Range("B" & Last_Row).value = wsSource.Range("H" & r).value
    wsDest.Range("C" & Last_Row).value = wsSource.Range("V" & r).value
    wsDest.Range("D" & Last_Row).value = wsSource.Range("X" & r).value
    wsDest.Range("H" & Last_Row).value = wsSource.Range("D" & r).value
    wsDest.Range("I" & Last_Row).value = wsSource.Range("E" & r).value
    wsDest.Range("N" & Last_Row).value = wsSource.Range("AE" & r).value
   
    
End If
End If
Next r

End With





End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Mitch,

as you're copying individual columns (rather than a block range all in one), there are few options.

One option would be to write your vba so that it re-arranges the columns you need from your source data to put them in order first. Then perform your searches / copies etc. and then move the columns back to the order you wanted them at the end once your loop has completed. This gives you the possibility to copy the entire range (lets say A:G if you had them in order) in one single line of code within your loop.

Another option would be for you to learn about Arrays, and perform your analysis "in memory" rather than on the worksheet itself - which is generally much faster - but requires a lot more thinking and effort. (But you might be able to do that now that you've written the above code, right? .. small steps ..etc.)

Its not clear to me if you are using the Application.ScreenUpdating = True/False statement around your code. If you use Application.ScreenUpdating = False right at the start of your code, and Application.ScreenUpdating = True right at the end of your code - it should help speed things up a little (even if you don't actually see anything working while its busy .. as it does what it says on the tin - it stops the screen from doing any flashing about while its running the code.

In a simillar vain, try using these at start and end of your code also - as this will stop Excel trying to do any calculations with every copy paste change you make to the sheet.

Application.Calculation = xlManual (at start)
Application.Calculation = xlAutomatic (at end of your code)

Then, I've declared your month and year now outside of your loop. This way they are fixed, and don't have to go off and calculate everytime you look at a new row.

all small steps, but overal should help speedup.

Rgds
Rob

VBA Code:
 month_now = Month(Now()) - 1
    year_now = Year(Now())
    
  lr = .Cells(Rows.Count, "A").End(xlUp).Row
  For r = lr To 2 Step -1
    If IsDate(.Range("A" & r).Value) Then
        If Month(.Range("A" & r).Value) = month_now And Year(.Range("A" & r).Value) = year_now Then
        Last_Row = Last_Row + 1
 
Upvote 0
Another option.
VBA Code:
Option Explicit
Sub Mitchx()
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim wsSrc As Worksheet, wsDest As Worksheet
    Set wsSrc = Worksheets("ATP-waarde bij inzet")
    Set wsDest = Worksheets("ATP combi")
    
    Dim SrcCols, DestCols
    SrcCols = Array(2, 4, 5, 8, 22, 24, 31)
    DestCols = Array(1, 2, 3, 4, 8, 9, 14)
    
    Dim lRow As Long, i As Long
    
    With wsSrc.Cells(1, 1).CurrentRegion
        .AutoFilter 1, 8, 11
        For i = LBound(SrcCols) To UBound(SrcCols)
            lRow = wsDest.Cells(Rows.Count, DestCols(i) + 1).End(3).Row + 1
            .Offset(1).Columns(SrcCols(i)).Copy wsDest.Cells(lRow, DestCols(i))
        Next i
        .AutoFilter
    End With
    
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
End Sub
 
Upvote 0
Another option.
VBA Code:
Option Explicit
Sub Mitchx()
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim wsSrc As Worksheet, wsDest As Worksheet
    Set wsSrc = Worksheets("ATP-waarde bij inzet")
    Set wsDest = Worksheets("ATP combi")
   
    Dim SrcCols, DestCols
    SrcCols = Array(2, 4, 5, 8, 22, 24, 31)
    DestCols = Array(1, 2, 3, 4, 8, 9, 14)
   
    Dim lRow As Long, i As Long
   
    With wsSrc.Cells(1, 1).CurrentRegion
        .AutoFilter 1, 8, 11
        For i = LBound(SrcCols) To UBound(SrcCols)
            lRow = wsDest.Cells(Rows.Count, DestCols(i) + 1).End(3).Row + 1
            .Offset(1).Columns(SrcCols(i)).Copy wsDest.Cells(lRow, DestCols(i))
        Next i
        .AutoFilter
    End With
   
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
End Sub
EDITED
VBA Code:
Option Explicit
Sub Mitchx()
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim wsSrc As Worksheet, wsDest As Worksheet
    Set wsSrc = Worksheets("ATP-waarde bij inzet")
    Set wsDest = Worksheets("ATP combi")
    
    Dim SrcCols, DestCols
    SrcCols = Array(2, 4, 5, 8, 22, 24, 31)
    DestCols = Array(1, 2, 3, 4, 8, 9, 14)
    
    Dim lRow As Long, i As Long
    
    With wsSrc.Cells(1, 1).CurrentRegion
        .AutoFilter 1, 8, 11
        For i = LBound(SrcCols) To UBound(SrcCols)
            lRow = wsDest.Cells(Rows.Count, DestCols(i)).End(3).Row + 1
            .Offset(1).Columns(SrcCols(i)).Copy wsDest.Cells(lRow, DestCols(i))
        Next i
        .AutoFilter
    End With
    
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
End Sub
 
Upvote 0
EDITED
VBA Code:
Option Explicit
Sub Mitchx()
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim wsSrc As Worksheet, wsDest As Worksheet
    Set wsSrc = Worksheets("ATP-waarde bij inzet")
    Set wsDest = Worksheets("ATP combi")
   
    Dim SrcCols, DestCols
    SrcCols = Array(2, 4, 5, 8, 22, 24, 31)
    DestCols = Array(1, 2, 3, 4, 8, 9, 14)
   
    Dim lRow As Long, i As Long
   
    With wsSrc.Cells(1, 1).CurrentRegion
        .AutoFilter 1, 8, 11
        For i = LBound(SrcCols) To UBound(SrcCols)
            lRow = wsDest.Cells(Rows.Count, DestCols(i)).End(3).Row + 1
            .Offset(1).Columns(SrcCols(i)).Copy wsDest.Cells(lRow, DestCols(i))
        Next i
        .AutoFilter
    End With
   
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
End Sub
Hi Kevin, I have tried the suggestion, the code gives no errors but it has no output for me.
Do you know how to incorporate the IF part in what Rob suggested? It does not paste any data as of now.
 
Upvote 0
Hi Kevin, I have tried the suggestion, the code gives no errors but it has no output for me.
Do you know how to incorporate the IF part in what Rob suggested? It does not paste any data as of now.
Just to confirm, the filter is set on column A of the source sheet to current month - 1. Is this correct?
It would help if you could provide some sample data using the XL2BB Tool :)

Here's what I got when I tested it with some dummy data, before:
Mitchx.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26Header27Header28Header29Header30Header31
21-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
32-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
43-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
54-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
65-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
76-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
81-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
92-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
103-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
114-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
125-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
ATP-waarde bij inzet


Mitchx.xlsm
ABCDEFGHIJKLMNOPQRS
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19
2
3
ATP combi


After:
Mitchx.xlsm
ABCDEFGHIJKLMNOPQRS
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19
2Col 2Col 4Col 5Col 8Col 22Col 24Col 31
3Col 2Col 4Col 5Col 8Col 22Col 24Col 31
4Col 2Col 4Col 5Col 8Col 22Col 24Col 31
5Col 2Col 4Col 5Col 8Col 22Col 24Col 31
6Col 2Col 4Col 5Col 8Col 22Col 24Col 31
7Col 2Col 4Col 5Col 8Col 22Col 24Col 31
8
ATP combi
 
Upvote 0
Just to confirm, the filter is set on column A of the source sheet to current month - 1. Is this correct?
It would help if you could provide some sample data using the XL2BB Tool :)

Here's what I got when I tested it with some dummy data, before:
Mitchx.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26Header27Header28Header29Header30Header31
21-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
32-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
43-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
54-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
65-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
76-JulCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
81-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
92-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
103-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
114-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
125-AugCol 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18Col 19Col 20Col 21Col 22Col 23Col 24Col 25Col 26Col 27Col 28Col 29Col 30Col 31
ATP-waarde bij inzet


Mitchx.xlsm
ABCDEFGHIJKLMNOPQRS
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19
2
3
ATP combi


After:
Mitchx.xlsm
ABCDEFGHIJKLMNOPQRS
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19
2Col 2Col 4Col 5Col 8Col 22Col 24Col 31
3Col 2Col 4Col 5Col 8Col 22Col 24Col 31
4Col 2Col 4Col 5Col 8Col 22Col 24Col 31
5Col 2Col 4Col 5Col 8Col 22Col 24Col 31
6Col 2Col 4Col 5Col 8Col 22Col 24Col 31
7Col 2Col 4Col 5Col 8Col 22Col 24Col 31
8
ATP combi

Hi Kevin,

I've tried the addon but it crashes my excel somehow and I cant seem to figure out why.
But I have tried something I deleted the original table so the destination sheet was blank. Accordingly, when I use your macro it now copies and pastes the correct info, so that works.

Nevertheless, I want the copied data to be appended to my current existing table. So the table of the destination sheet must leave the data that is already there intact. And then appending the newly copied data to the table.

If you require more information feel free to ask. And many thanks this far, I appreciate the time and help!
 
Upvote 0
Unfortunately, I'm not sure that I can help you any further. Here's what happens when I run the code in post #4 using the dummy data created in post #6

Mitchx.xlsm
ABCDEFGHIJKLMNOPQRS
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19
2Col 2Col 4Col 5Col 8Col 22Col 24Col 31
3Col 2Col 4Col 5Col 8Col 22Col 24Col 31
4Col 2Col 4Col 5Col 8Col 22Col 24Col 31
5Col 2Col 4Col 5Col 8Col 22Col 24Col 31
6Col 2Col 4Col 5Col 8Col 22Col 24Col 31
7Col 2Col 4Col 5Col 8Col 22Col 24Col 31
8Col 2Col 4Col 5Col 8Col 22Col 24Col 31
9Col 2Col 4Col 5Col 8Col 22Col 24Col 31
10Col 2Col 4Col 5Col 8Col 22Col 24Col 31
11Col 2Col 4Col 5Col 8Col 22Col 24Col 31
12Col 2Col 4Col 5Col 8Col 22Col 24Col 31
13Col 2Col 4Col 5Col 8Col 22Col 24Col 31
14
ATP combi


As you can see, it added new data to the bottom of the existing data without overwriting it. Can you share the file via a file sharing site? Otherwise, I'm a bit stuck :unsure:
 
Upvote 0
Unfortunately, I'm not sure that I can help you any further. Here's what happens when I run the code in post #4 using the dummy data created in post #6

Mitchx.xlsm
ABCDEFGHIJKLMNOPQRS
1Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19
2Col 2Col 4Col 5Col 8Col 22Col 24Col 31
3Col 2Col 4Col 5Col 8Col 22Col 24Col 31
4Col 2Col 4Col 5Col 8Col 22Col 24Col 31
5Col 2Col 4Col 5Col 8Col 22Col 24Col 31
6Col 2Col 4Col 5Col 8Col 22Col 24Col 31
7Col 2Col 4Col 5Col 8Col 22Col 24Col 31
8Col 2Col 4Col 5Col 8Col 22Col 24Col 31
9Col 2Col 4Col 5Col 8Col 22Col 24Col 31
10Col 2Col 4Col 5Col 8Col 22Col 24Col 31
11Col 2Col 4Col 5Col 8Col 22Col 24Col 31
12Col 2Col 4Col 5Col 8Col 22Col 24Col 31
13Col 2Col 4Col 5Col 8Col 22Col 24Col 31
14
ATP combi


As you can see, it added new data to the bottom of the existing data without overwriting it. Can you share the file via a file sharing site? Otherwise, I'm a bit stuck :unsure:
Hi Kevin,

Okay, somehow it copy and pastes now. I had to re-arrange the arrays to copy the correct data but I got that fixed.

But now the major problem is this what it seems to do is this. I know this is really hard to see but that doens't matter. Basically what it is doing is copying column A correctly into the table.
But the next column it puts after copied data. So a range of 130 gets extended to 130 * 7 arrays.

What it does is Putting column A from range 2 till 130, then column B from 131 till 260 etc.. But I wish to get them next to each other but in their respective columns.

Thanks again!

1661165387036.png
 
Upvote 0
Hi Kevin,

Okay, somehow it copy and pastes now. I had to re-arrange the arrays to copy the correct data but I got that fixed.

But now the major problem is this what it seems to do is this. I know this is really hard to see but that doens't matter. Basically what it is doing is copying column A correctly into the table.
But the next column it puts after copied data. So a range of 130 gets extended to 130 * 7 arrays.

What it does is Putting column A from range 2 till 130, then column B from 131 till 260 etc.. But I wish to get them next to each other but in their respective columns.

Thanks again!

View attachment 72175
Could you post the entire code you used to create that image please?
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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