Copy data from entire column into specific table

scorziello

New Member
Joined
Jun 11, 2021
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
I am trying to copy data from Sheet2 column G to Table1 Column3 on Sheet1. Here is the code I have so far but keep getting an error
VBA Code:
Sub moveStuff()
Dim lr As Long
lr = Sheets("Sheet1").Cells(Rows.Count, "G").End(xlUp).Row
Sheets("Sheets1").Range("G2:G" & lr).Copy Sheets("Sheet2").ListObjects("Table5").ListColumns(3)
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Edit: I am specifying which table because Sheet1 will have 3 tables when it is done.
 
Upvote 0
I am trying to copy data from Sheet2 column G to Table1 Column3 on Sheet1.
If this description is correct then for one thing you have the sheets in your code the wrong way around as it is trying to copy from Sheet1 column G to a table on Sheet2.
So, based on the word description above, try this

VBA Code:
Sub moveStuff()
  Dim lr As Long
  
  lr = Sheets("Sheet2").Cells(Rows.Count, "G").End(xlUp).Row
  Sheets("Sheet2").Range("G2:G" & lr).Copy Sheets("Sheet1").ListObjects("Table5").ListColumns(3).DataBodyRange.Cells(1)
End Sub
 
Upvote 0
Solution
If this description is correct then for one thing you have the sheets in your code the wrong way around as it is trying to copy from Sheet1 column G to a table on Sheet2.
So, based on the word description above, try this

VBA Code:
Sub moveStuff()
  Dim lr As Long
 
  lr = Sheets("Sheet2").Cells(Rows.Count, "G").End(xlUp).Row
  Sheets("Sheet2").Range("G2:G" & lr).Copy Sheets("Sheet1").ListObjects("Table5").ListColumns(3).DataBodyRange.Cells(1)
End Sub
Thank you for the reply, and sorry for the error. I am still getting a Subscript out of range error when I run it with your changes
 
Upvote 0
In your text you had Table1 but in your code you had Table5.
Peter has opted to use Table5 as the name in the code. If this is not correct substitute the real Table name and see if that fixes it.
 
Upvote 0
Copy the code below immediately after the Dim lr line and tell us which line is highlighted in yellow when you get the error message.

VBA Code:
  Debug.Print Sheets("Sheet1").Name
  Debug.Print Sheets("Sheet2").Name
  Debug.Print Sheets("Sheet1").ListObjects("Table5").ListColumns(3).DataBodyRange.Address
 
Upvote 0
Getting the error with
Sheets("Sheets2").Range("G2:G" & lr).Copy Sheets("Sheet1").ListObjects("Table5").ListColumns(3).DataBodyRange.Cells(1)
 
Upvote 0
Did you copy the code I gave you to the beginning of your code and then run the code ?
I would have expected it to error out on one of those lines and not make it as far as the line you are indicating.
 
Upvote 0
Shouldn't this
Rich (BB code):
Sheets("Sheets2")
BE
Rich (BB code):
Sheets("Sheet2")
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,693
Members
449,331
Latest member
smckenzie2016

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