Paste data to last row in table

FlashNZ

New Member
Joined
Mar 3, 2021
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a basic Macro that copies some data from one table and pastes it into another table. It works fine except that it pastes the data into the first row of Tbl_Schedule and overwrites the existing data. I want it to paste the new data into the bottom or last row in the table. My Table where I want the data pasted is called Tbl_Schedule on Sheet1.
Appreciate any help.

Sub CopyToSchedule()
'
' CopyToSchedule Macro
'

'
Range(Cells(Selection.Row, 1), Cells(Selection.Row, 7)).Select
Selection.Copy
Sheets("Sheet1").Select
Range("Tbl_Schedule[Inquiry Number]").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome to the board!

Here's my suggestion:
VBA Code:
Sub WriteValues()

Dim Rng As Range

'Sets the Rng range to be the columns A:G of the selected rows:
Set Rng = Intersect(Selection.EntireRow, Columns("A:G"))

With Sheets("Sheet1")
    With .ListObjects("Tbl_Schedule").DataBodyRange 'The data rows of the table
        'Counts the rows in the table, starts from the next row in the first column and writes the values from the selected rows
        .Cells(.Rows.Count, 1).Offset(1).Resize(Rng.Rows.Count, 7).Value = Rng.Value
    End With
End With

End Sub
The code skips unnecessary selections and calculates the place you want to copy the values to. And write the values instead of actually copying them.
 
Upvote 0
Sub WriteValues() Dim Rng As Range 'Sets the Rng range to be the columns A:G of the selected rows: Set Rng = Intersect(Selection.EntireRow, Columns("A:G")) With Sheets("Sheet1") With .ListObjects("Tbl_Schedule").DataBodyRange 'The data rows of the table 'Counts the rows in the table, starts from the next row in the first column and writes the values from the selected rows .Cells(.Rows.Count, 1).Offset(1).Resize(Rng.Rows.Count, 7).Value = Rng.Value End With End With End Sub
Hi Misca,
Thanks, worked perfectly:)
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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