Copy two different tables into lastrow in one table in a new sheet

mrmcoupe

New Member
Joined
Jan 22, 2017
Messages
17
Hei,

I´ve tweaked a code to copy/paste two different dynamic tables into one dynamic table in another sheet. However, it seems that the latter part of the code (copying the last table) overwrites the first one. I´ve used lastrow to determine the last cell with data, but it failes.

Code:
   Sub copyall()








    Dim Last_Row1 As Long, Last_Row2 As Long, Last_Row3 As Long
    Dim WB1 As Workbook, WB2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim cc As Range


    Set WB1 = ThisWorkbook ' Workbook where you want to copy the data
    Set ws1 = WB1.Sheets("Sheet1") ' Change the name of your Sheet
     Set ws2 = WB1.Sheets("Sheet2") ' Change the name of your Sheet
    Set cc = WB1.Sheets("Sheet2").Range("D4:f100")
    
    
    
    cc.ClearContents


    Last_Row1 = ws1.Range("L" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
    Last_Row3 = ws1.Range("F" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
    
    Last_Row2 = ws2.Range("D" & Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data
    
  




    ws1.Range("F5:H" & Last_Row3).Copy ws2.Range("D" & Last_Row2)
    ws1.Range("L5:N" & Last_Row1).Copy ws2.Range("D" & Last_Row2)
    




End Sub

Any suggestions on how to avoid overwriting?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
try to change this:

Rich (BB code):
    Last_Row1 = ws1.Range("L" & ws1.Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
    Last_Row3 = ws1.Range("F" & ws1.Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
    
    Last_Row2 = ws2.Range("D" & ws2.Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data
    ws1.Range("F5:H" & Last_Row3).Copy ws2.Range("D" & Last_Row2)
    ws1.Range("L5:N" & Last_Row1).Copy ws2.Range("D" & Last_Row2)
 
Last edited:
Upvote 0
Hi, thanks for very fast reply!

Unfortunately, this will give the exact same problem, and in addition, the data is copied one row lower down due to the +1 in the two last code bits. :(

Other suggestions?
 
Upvote 0
where is the last row in you document? coz you delete all data from 4 to 100 row by this
Code:
 cc.ClearContents
 
Upvote 0
forget all i told before
just insert
Code:
     Last_Row2 = ws2.Range("D" & ws2.Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data
    ws1.Range("F5:H" & Last_Row3).Copy ws2.Range("D" & Last_Row2)
     Last_Row2 = ws2.Range("D" & ws2.Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data
    ws1.Range("L5:N" & Last_Row1).Copy ws2.Range("D" & Last_Row2)
 
Upvote 0
Ok, so this code clears the data from the the spreadsheet first. If I pause this, the data copied to new table (sheet 2) will accumulate, but only with the data from table two in sheet1. So, the problem is the same.

The data from the first code is overwritten

Code:
ws1.Range("F5:H" & Last_Row3).Copy ws2.Range("D" & Last_Row2) 'this is overwritten
ws1.Range("L5:N" & Last_Row1).Copy ws2.Range("D" & Last_Row2) ' by this
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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