vba copy from one table to another

ldarley

Board Regular
Joined
Apr 10, 2012
Messages
106
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
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
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
 
Upvote 0
Solution
Awesome thanks Alpha frog great learning point for me about the clipboard!
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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