Copy Paste From Table to Table

pgspringer

New Member
Joined
Nov 10, 2014
Messages
4
Hello,

I am working on a To-Do list in Excel 2010. I am running into a paste range class invalid error. I am relatively new to VBA working off macros and google.

The part I am having trouble with is moving the completed tasks to another table in the sheet, the code I currently have is this

Sub MoveCompleted()
Dim LastRow As Variant
Set LastRow = ActiveSheet.ListObjects("Table710").ListRows.Add

ActiveSheet.ListObjects("Table7").Range.AutoFilter Field:=5, Criteria1:="Completed"
ActiveSheet.ListObjects("Table7").DataBodyRange.Copy
ActiveSheet.ListObjects("Table7").Range.AutoFilter Field:=5
LastRow.Range.PasteSpecial xlPasteValues
End Sub


Right now I am not worried about deleting the original data from Table7, I figured I would worry about that later.

Everything works up until paste. My thought is because I am only adding one row and the data copied is potentially more than one, but I do not have the knowledge of how to proceed. Any help would be wonderfully obliged.

Thank You!

Also working on Windows 7 - 64bit.


 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Well this is what I found:

Rich (BB code):
Sub MoveTableStuff()
Dim loSource As Excel.ListObject
Dim loTarget As Excel.ListObject
Dim SourceDataRowsCount As Long
Dim TargetDataRowsCount As Long


Set loSource = ActiveSheet.ListObjects("Table7")
Set loTarget = ActiveSheet.ListObjects("Table710")
With loSource
    .Range.AutoFilter Field:=5, Criteria1:="Completed"
    SourceDataRowsCount = .ListColumns(1).DataBodyRange.SpecialCells(xlCellTypeVisible).Count
End With
With loTarget
    TargetDataRowsCount = .DataBodyRange.Rows.Count
    .Resize .Range.Resize(.Range.Rows.Count + SourceDataRowsCount, .Range.Columns.Count)
    loSource.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
    .DataBodyRange.Cells(TargetDataRowsCount + 1, 1).PasteSpecial (xlPasteValues)
    Application.CutCopyMode = False
End With
With loSource
    .Range.AutoFilter Field:=5
    .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
End With
End Sub

So this is filtering data from table 1, counting how many rows there are, copying it, adding - or resizing I guess, the second table to how many rows were copied, and then pasting the values (with out error because there are rows for the data to go in to), then deleting the others (which I don't quite understand yet) - am I correct?


If someone would like to take the time to help me understand this part:

Rich (BB code):
.DataBodyRange.Cells(TargetDataRowsCount + 1, 1)

Of this:

Rich (BB code):
TargetDataRowsCount = .DataBodyRange.Rows.Count    .Resize .Range.Resize(.Range.Rows.Count + SourceDataRowsCount, .Range.Columns.Count)
    loSource.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
    .DataBodyRange.Cells(TargetDataRowsCount + 1, 1).PasteSpecial (xlPasteValues)
    Application.CutCopyMode = False

That would be awesome!

Also, Source: excel vba - How to select table rows/complete table? - Stack Overflow

Thanks
 
Last edited:
Upvote 0
Annnnnd Final Code that moves completed tasks from one table to another, and removing them from the first....:

Code:
Sub MoveTableStuff()
Dim loSource As Excel.ListObject
Dim loTarget As Excel.ListObject
Dim SourceDataRowsCount As Long
Dim TargetDataRowsCount As Long

Set loSource = ActiveSheet.ListObjects("Table7")
Set loTarget = ActiveSheet.ListObjects("Table710")
With loSource
    .Range.AutoFilter Field:=5, Criteria1:="Completed"
    SourceDataRowsCount = .ListColumns(1).DataBodyRange.SpecialCells(xlCellTypeVisible).Count
End With
With loTarget
    TargetDataRowsCount = .DataBodyRange.Rows.Count
    .Resize .Range.Resize(.Range.Rows.Count + SourceDataRowsCount, .Range.Columns.Count)
    loSource.DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
    .DataBodyRange.Cells(TargetDataRowsCount + 1, 1).PasteSpecial (xlPasteValues)
    Application.CutCopyMode = False
End With
With loSource
    .DataBodyRange.SpecialCells(xlCellTypeVisible).ClearContents
        .Range.AutoFilter Field:=5
    .Sort.SortFields.Add _
        Key:=Range("Table7[[#All],[Status]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    .Resize .Range.Resize(.Range.Rows.Count - SourceDataRowsCount, .Range.Columns.Count)
End With
End Sub
 
Upvote 0
Thanks for posting this! I went through a few other sites/links, but this one worked perfectly for what I needed to do!
 
Upvote 0

Forum statistics

Threads
1,215,620
Messages
6,125,876
Members
449,268
Latest member
sGraham24

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