vba copy from one table to another

ldarley

Board Regular
Joined
Apr 10, 2012
Messages
104
Office Version
  1. 2019
  2. 2016
Hi,

I'm getting a getting a runtime error 1004 on this line tbl2.ListRows(x).Range.PasteSpecial xlPasteAll (full code below). I've tried a few variations on this and can't seem to get the copied table row to paste into my second table. The range size is exactly the same and they are one two different worksheets. Can anyone point out where I've gone wrong?

Many Thanks

VBA Code:
Dim ProjectID As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim tbl1 As ListObject
Dim tbl2 As ListObject
Dim CopyRow As Range
Dim i As Long
Dim x As Long  

        'set up variables
        Set ws1 = Projects
        Set tbl1 = ws1.ListObjects("ProjectTbl")
        Set ws2 = Closed
        Set tbl2 = ws2.ListObjects("ClosedTbl")
        ProjectID = UserForm2.ProjectIDCmb.Value

        i = Application.WorksheetFunction.Match(ProjectID, tbl1.ListColumns(1).DataBodyRange, 0)
        Set CopyRow = tbl1.ListRows(i).Range
        CopyRow.Copy
  
        Set newrow = tbl2.ListRows.Add
        x = tbl2.ListRows.Count
        tbl2.ListRows(x).Range.PasteSpecial xlPasteAll
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,379
When you do a .ListRows.Add to a table, it clears the copied range from the clipboard. Then there is nothing to PasteSpecial any more. Move your .Copy command to after the .ListRows.Add

VBA Code:
        i = Application.WorksheetFunction.Match(ProjectID, tbl1.ListColumns(1).DataBodyRange, 0)
        'Set CopyRow = tbl1.ListRows(i).Range
        'CopyRow.Copy
  
        Set newrow = tbl2.ListRows.Add
        'x = tbl2.ListRows.Count
        tbl1.ListRows(i).Range.Copy
        newrow.Range.PasteSpecial xlPasteAll

Alternatively, you could use the Range.Value = Range.Value method or the Range.Copy Destination:=Range method
 
Solution

ldarley

Board Regular
Joined
Apr 10, 2012
Messages
104
Office Version
  1. 2019
  2. 2016
Awesome thanks Alpha frog great learning point for me about the clipboard!
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,379
As a general rule when using PasteSpecial, try to place the Copy command immediately before the PasteSpecial command.

You're welcome and thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,589
Messages
5,597,053
Members
414,118
Latest member
moversnpackers

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
Top