Add multi-row range at the end of a Table(Listobject)

olorin_117

New Member
Joined
Jan 19, 2022
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
Hello

I have some data in a worksheet in columns A through E...The rows of data are changing..Some day the range may be A5:E10, another day A5:E30..These data are not in a table (listobject).I am trying to paste them in a table (listobject) in another workbook.I have managed to paste them without calling the listobject method but i am trying to do so..The code I have is this

VBA Code:
Private Sub copypastelines()

Dim rng As Range

montpth = ("filepath.xlsx")

Set rng = ActiveSheet.Range("A5:E" & Range("A5").End(xlDown).Row)
rng.Copy

Workbooks.Open (montpth), UpdateLinks:=False
With ActiveWorkbook

next_row = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
Sheets("Sheet1").Cells(next_row, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
      
End With
      
ActiveWorkbook.Close True

End Sub

I know there is a way to add the data with the listobject.add method but I haven't figure out how
I am using Excel 2019
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try the following macro . . .

VBA Code:
Private Sub copypastelines()

    Dim montpth As String
    montpth = "c:\path\filename.xlsx" 'change the path and filename accordingly
    
    Dim rng As Range
    Set rng = Range("A5:E" & Range("A5").End(xlDown).Row)

    Dim wb As Workbook
    Set wb = Workbooks.Open(montpth, UpdateLinks:=False)
    
    Dim newListRow As ListRow
    Set newListRow = wb.Worksheets("Sheet1").ListObjects("Table1").ListRows.Add
    
    rng.Copy
    
    newListRow.Range.PasteSpecial Paste:=xlPasteValues
    
    Application.CutCopyMode = False
          
    wb.Close True

End Sub

Hope this helps!
 
Upvote 0
Solution
Try the following macro . . .

VBA Code:
Private Sub copypastelines()

    Dim montpth As String
    montpth = "c:\path\filename.xlsx" 'change the path and filename accordingly
   
    Dim rng As Range
    Set rng = Range("A5:E" & Range("A5").End(xlDown).Row)

    Dim wb As Workbook
    Set wb = Workbooks.Open(montpth, UpdateLinks:=False)
   
    Dim newListRow As ListRow
    Set newListRow = wb.Worksheets("Sheet1").ListObjects("Table1").ListRows.Add
   
    rng.Copy
   
    newListRow.Range.PasteSpecial Paste:=xlPasteValues
   
    Application.CutCopyMode = False
         
    wb.Close True

End Sub

Hope this helps!
This worked perfectly AND found out what I was doing wrong..In the context of pasting my data in the Table, I had to put the
VBA Code:
rng.Copy
inside the open workbook-target..I tried 1000 different approaches but with this command outside the scope of the open workbook- target and everytime I got the same error code that I did not know how to work around..Thanks a lot man
 
Upvote 0
Yeah, this line of code . . .

VBA Code:
Set newListRow = wb.Worksheets("Sheet1").ListObjects("Table1").ListRows.Add

. . . seems to clear the clipboard. Anyway, glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

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