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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,384
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,384
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,123,381
Messages
5,601,302
Members
414,440
Latest member
Kim0204

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