Copy data to the first row of table

ChanL

Board Regular
Joined
Apr 8, 2021
Messages
65
Office Version
  1. 2019
Platform
  1. Windows
I have created the following VBA for user to copy data from Sheet "fromtext" to sheet"report" which contain a table that I created (the table is created with ctrl + T , not ordinary range table.

There will always be time users will need to duplicate the workbook to compile data for a brand new year.
So for this, after a new duplicated workbook is created, they will clear the content in the table and start run the VBA again.
But the problem is, everytime after the table cleared, the data always pasted at the last row of table which automatically make the data cannot fit into the table nicely.
So , I thinking of modify my current code, but I need advice.


Book3.xlsx
ABCDEFGHIJ
1Order IDShip DateOrder DateShip ModeCustomer IDCustomer NameDestinationContact NumberStatusSegment
2
3CA-2014-10589311/18/201411/11/2014Standard ClassPK-19075Pete KrizNew York123456On the WayConsumer
4CA-2014-1671645/15/20145/13/2014Second ClassAG-10270Alejandro GroveWashington123456FailedConsumer
5CA-2014-1433369/1/20148/27/2014Second ClassZD-21925Zuschuss DonatelliNew Zealand123456DeliveredConsumer
6CA-2014-1433369/1/20148/27/2014Second ClassZD-21925Zuschuss DonatelliNew York123456On the WayConsumer
7CA-2014-1433369/1/20148/27/2014Second ClassZD-21925Zuschuss DonatelliWashington123456DeliveredConsumer
8CA-2016-13733012/13/201612/9/2016Standard ClassKB-16585Ken BlackJapan123456On the WayCorporate
9CA-2016-13733012/13/201612/9/2016Standard ClassKB-16585Ken BlackNew York123456DeliveredCorporate
10US-2017-1569097/18/20177/16/2017Second ClassSF-20065Sandra FlanaganWashington123456On the WayConsumer
11CA-2015-1063209/30/20159/25/2015Standard ClassEB-13870Emily BurnsNew Zealand123456On the WayConsumer
12CA-2016-1217551/20/20161/16/2016Second ClassEH-13945Eric HoffmannNew York123456FailedConsumer
13CA-2016-1217551/20/20161/16/2016Second ClassEH-13945Eric HoffmannWashington123456On the WayConsumer
14US-2015-1506309/21/20159/17/2015Standard ClassTB-21520Tracy BlumsteinNew Zealand123456DeliveredConsumer
15US-2015-1506309/21/20159/17/2015Standard ClassTB-21520Tracy BlumsteinNew York123456On the WayConsumer
16US-2015-1506309/21/20159/17/2015Standard ClassTB-21520Tracy BlumsteinWashington123456DeliveredConsumer
Sheet1


VBA Code:
With Sheets ("fromtext")
  . range("A9",.range("A9").end(xlDown).End(xltoright).copy
end with

With sheets("report").listobjects(1).range
   .cells(.rows.count+1,1).pastespecial xlvalues
end with

application.cutcopymode=false
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Can you upload the workbook?

There are numerous problems with the line of code:
VBA Code:
. range("A9",.range("A9").end(xlDown).End(xltoright).copy
 
Upvote 0
You have some typos in that posted code (always better to copy/paste your actual working code rather than re-typing it in the forum) but try this with a copy of your workbook.

VBA Code:
Dim NextRow As Long

With Sheets("fromtext")
  .Range("A9", .Range("A9").End(xlDown).End(xlToRight)).Copy
End With

With Sheets("report").ListObjects(1).Range
  If .SpecialCells(xlConstants).Count = .Columns.Count Then
    NextRow = 2
  Else
    NextRow = .Rows.Count + 1
  End If
  .Cells(NextRow, 1).PasteSpecial xlValues
End With

Application.CutCopyMode = False
 
Upvote 0
Solution
You have some typos in that posted code (always better to copy/paste your actual working code rather than re-typing it in the forum) but try this with a copy of your workbook.

VBA Code:
Dim NextRow As Long

With Sheets("fromtext")
  .Range("A9", .Range("A9").End(xlDown).End(xlToRight)).Copy
End With

With Sheets("report").ListObjects(1).Range
  If .SpecialCells(xlConstants).Count = .Columns.Count Then
    NextRow = 2
  Else
    NextRow = .Rows.Count + 1
  End If
  .Cells(NextRow, 1).PasteSpecial xlValues
End With

Application.CutCopyMode = False
thanks for the advise, but the actual code is at my working laptop and there is a restriction for staff to share files to external sources. So, it leave me no choice but to re-type everything. I will try this code first
 
Upvote 0
Hi, do you mind to share how to upload workbook to this platform? As xl2bb extension only can upload range of table
 
Upvote 0
Hi, do you mind to share how to upload workbook to this platform?
This forum does not allow upload of actual workbooks, that is why we have XL2BB

As xl2bb extension only can upload range of table
We generally find that sufficient & I note in another thread you have it working.

Did you try my code yet?
 
Upvote 0
This forum does not allow upload of actual workbooks, that is why we have XL2BB


We generally find that sufficient & I note in another thread you have it working.

Did you try my code yet?

This forum does not allow upload of actual workbooks, that is why we have XL2BB


We generally find that sufficient & I note in another thread you have it working.

Did you try my code yet?
i tried your code. It work perfectly on my dummy data , but it does not work on my actual data that is in my office laptop. I wonder if the code that I used before this copypaste code got any effect on it?
 
Upvote 0
This forum does not allow upload of actual workbooks, that is why we have XL2BB
@Peter_SSs is correct there. But you can google 'Free file upload' and post the link back here. I recently found gofile.io
it offers unlimitted access as far as upload file size
 
Upvote 0
thanks for reply. @Peter_SSs code work perfectly on my dummy data that I created to test the code at my personal laptop. But when I use the same code for my actual data, it doesn't work. I will put in the code that I used before the copy paste code here . Hope to get some advice from both of you.
@Peter_SSs is correct there. But you can google 'Free file upload' and post the link back here. I recently found gofile.io
it offers unlimitted access as far as upload file size
 
Upvote 0

Forum statistics

Threads
1,215,160
Messages
6,123,355
Members
449,097
Latest member
thnirmitha

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