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
 
Could you post the entire code you used to create that image please?
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, 8, 22, 24, 4, 5, 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

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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, 8, 22, 24, 4, 5, 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
When deleting all the data it correctly puts the columns next to each other, however with my existing table its gets added next to each other
 
Upvote 0
@kevin9999

This is how my destination sheet looks intact. I only need to copy paste the specific columns I have in the array now. Other data comes from different sources (so it basically skips columns sometimes)
The first column I have renamed test1 because of privacy (it has license plates)

1661166148007.png
 
Upvote 0
@kevin9999

This is how my destination sheet looks intact. I only need to copy paste the specific columns I have in the array now. Other data comes from different sources (so it basically skips columns sometimes)
The first column I have renamed test1 because of privacy (it has license plates)

View attachment 72178

This is how it translates to the source sheet

Destination sheet vs Source sheet
Column A = A
Column B = H
Column C = V
Column D = X
Column H = D
Column I = E
Column N = AE

I know the order is strange, but I hope it gives some clarification.
 
Upvote 0
@kevin9999

This is how my destination sheet looks intact. I only need to copy paste the specific columns I have in the array now. Other data comes from different sources (so it basically skips columns sometimes)
The first column I have renamed test1 because of privacy (it has license plates)

View attachment 72178
OK - some clarifications:
1. You said in post # 1 that you wanted this: The data must be appended to the last row of the destination table. I took this to mean that the new data from each column of the source sheet was to be appended to the bottom of any existing data in the destination sheet - in the particular column where you wanted the new data pasted. Is this still the case?

2. The description in post #14 doesn't match what we have done so far. The code (from your original in post #1 to the latest in post #11) has column B of the source sheet going to column A of the destination sheet, not as you say in post #14:
Destination sheet vs Source sheet
Column A = A (?)

Which is it?

3. I can't tell from you image which column your data starts in. Does both the source and destination sheet data start in column A?
 
Upvote 0
OK - some clarifications:
1. You said in post # 1 that you wanted this: The data must be appended to the last row of the destination table. I took this to mean that the new data from each column of the source sheet was to be appended to the bottom of any existing data in the destination sheet - in the particular column where you wanted the new data pasted. Is this still the case?

2. The description in post #14 doesn't match what we have done so far. The code (from your original in post #1 to the latest in post #11) has column B of the source sheet going to column A of the destination sheet, not as you say in post #14:
Destination sheet vs Source sheet
Column A = A (?)

Which is it?

3. I can't tell from you image which column your data starts in. Does both the source and destination sheet data start in column A?

Point 1, yes that is required.
Point 2 , I am sorry - Column A from destination = Column B from source (my mistake)
Point 3, Source sheet starts at column A (this is where the date is) column B from source is license plate. Destination sheet starts at Column A (license plate)

Therefore A dest = B source..

Sorry for the confusion.
 
Upvote 0
Try moving this line to before the For statement For i = LBound(SrcCols) To UBound(SrcCols) and hard coding the column as "A"
VBA Code:
            lRow = wsDest.Cells(Rows.Count, "A").End(3).Row + 1
 
Upvote 0
Mitch, it looks like a potential problem (based on your image) is that the last row may differ per column in the destination sheet (I can see some empty cells there). This would cause the imported data from the source sheet to be out of alignment. Try this amended code & see if it comes closer to what you want. It find the last row just once and uses it for all copied data.
That's about me done for the day, let me know how this new code works, and I'll relook at it with a clear head tomorrow.
Cheers :)

VBA Code:
Sub Mitchx_2()
    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, 8, 22, 24, 4, 5, 31)
    DestCols = Array(1, 2, 3, 4, 8, 9, 14)
    
    Dim lRow As Long, i As Long
    lRow = wsDest.Cells.Find("*", , xlFormulas, , 1, 2).Row + 1
    With wsSrc.Cells(1, 1).CurrentRegion
        .AutoFilter 1, 8, 11
        For i = LBound(SrcCols) To UBound(SrcCols)
            .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
Solution
Mitch, it looks like a potential problem (based on your image) is that the last row may differ per column in the destination sheet (I can see some empty cells there). This would cause the imported data from the source sheet to be out of alignment. Try this amended code & see if it comes closer to what you want. It find the last row just once and uses it for all copied data.
That's about me done for the day, let me know how this new code works, and I'll relook at it with a clear head tomorrow.
Cheers :)

VBA Code:
Sub Mitchx_2()
    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, 8, 22, 24, 4, 5, 31)
    DestCols = Array(1, 2, 3, 4, 8, 9, 14)
   
    Dim lRow As Long, i As Long
    lRow = wsDest.Cells.Find("*", , xlFormulas, , 1, 2).Row + 1
    With wsSrc.Cells(1, 1).CurrentRegion
        .AutoFilter 1, 8, 11
        For i = LBound(SrcCols) To UBound(SrcCols)
            .Offset(1).Columns(SrcCols(i)).Copy wsDest.Cells(lRow, DestCols(i))
        Next i
        .AutoFilter
    End With
   
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
End Sub

Well your final suggestion has worked for me, haha. It is being imported correctly now!!!

Thank you so much for your patience and suggestions. I have learned something new today!

Have a great day!
 
Upvote 0
Well your final suggestion has worked for me, haha. It is being imported correctly now!!!

Thank you so much for your patience and suggestions. I have learned something new today!

Have a great day!
Thanks Mitch, glad we were able to get there in the end 😁
 
Upvote 0

Forum statistics

Threads
1,215,042
Messages
6,122,810
Members
449,095
Latest member
m_smith_solihull

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