Paste data to last row in table

FlashNZ

New Member
Joined
Mar 3, 2021
Messages
8
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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,691
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.
 

FlashNZ

New Member
Joined
Mar 3, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
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:)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,981
Messages
5,639,368
Members
417,083
Latest member
vijaykrrao

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