Copy down to last row in a table

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I have a table named "TableForecast". The table headers are on row 6 I want to add a button where when clicked it copies the data from two fields in the table (Date, Qty) from the first table row (7) down to the last row in the table. Where I am lost is that the table can and will be various sizes (number of rows). It can be based on the first column ("Row #") to determine the where the last row is. Reminder this is a table in excel. Thanks for the help
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi @gheyman , assuming that you will always have blank cells in Date and Qty columns before running this copy and paste code, your code will be:
VBA Code:
Sub Macro1() 'name your macro here
Application.ScreenUpdating = False

ActiveSheet.ListObjects("Table1").ListColumns("Date").Range(1, 1).Select 'replace "Table1" with your table name.
Selection.End(xlDown).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

ActiveSheet.ListObjects("Table1").ListColumns("Qty").Range(1, 1).Select 'replace "Table1" with your table name.
Selection.End(xlDown).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

ActiveSheet.ListObjects("Table1").Range(1, 1).Select 'replace "Table1" with your table name.

Application.ScreenUpdating = True
End Sub

After this, you can add a button from Develop tab and assign this marco to that button. Hope this helps.
 
Upvote 0
Hi @gheyman , assuming that you will always have blank cells in Date and Qty columns before running this copy and paste code, your code will be:
VBA Code:
Sub Macro1() 'name your macro here
Application.ScreenUpdating = False

ActiveSheet.ListObjects("Table1").ListColumns("Date").Range(1, 1).Select 'replace "Table1" with your table name.
Selection.End(xlDown).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

ActiveSheet.ListObjects("Table1").ListColumns("Qty").Range(1, 1).Select 'replace "Table1" with your table name.
Selection.End(xlDown).Select
Selection.Copy
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

ActiveSheet.ListObjects("Table1").Range(1, 1).Select 'replace "Table1" with your table name.

Application.ScreenUpdating = True
End Sub

After this, you can add a button from Develop tab and assign this marco to that button. Hope this helps.
Thanks! Really appreciate the help. Is there anyway to make this to where it only copies the Date and Qty down to the last row in the table where there is data in the first column ("Row #"). So in the column in the table that is named "Row #" if I only have data in the first five cells (rows) - it only copies the data down to there and not the last row in the table?
 
Upvote 0
Hi @gheyman , try this
VBA Code:
Sub Macro1() 'name your macro here
Application.ScreenUpdating = False

Dim rowadrs As Long
Dim coladrs As Long
ActiveSheet.ListObjects("Table1").ListColumns("Date").Range(1, 1).Select 'replace "Table1" with your table name.
coladrs = ActiveCell.Column
Selection.End(xlDown).Select
Selection.Copy
ActiveSheet.ListObjects("Table1").ListColumns("Row #").Range(1, 1).Select 'replace "Table1" with your table name.
Selection.End(xlDown).Select
rowadrs = ActiveCell.Row
ActiveSheet.Cells(rowadrs, coladrs).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

ActiveSheet.ListObjects("Table1").ListColumns("Qty").Range(1, 1).Select 'replace "Table1" with your table name.
coladrs = ActiveCell.Column
Selection.End(xlDown).Select
Selection.Copy
ActiveSheet.ListObjects("Table1").ListColumns("Row #").Range(1, 1).Select 'replace "Table1" with your table name.
Selection.End(xlDown).Select
rowadrs = ActiveCell.Row
ActiveSheet.Cells(rowadrs, coladrs).Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False

ActiveSheet.ListObjects("Table1").Range(1, 1).Select 'replace "Table1" with your table name.

Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,795
Messages
6,121,624
Members
449,041
Latest member
Postman24

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