2

I am trying to copy the contents of one table to another. in the process I am copying only some columns. Sorry if my question is too basic as I'm new to Excel VBA

Dim tableName As ListObject
Set tableName = Worksheets("DataSource").ListObjects(1)
[Table1[TaskUID]].Copy [tableName[TaskUID]]

Line 3 is throwing an error "Object Required"

Could anyone please help with the syntax

4
  • Are you trying to copy Table1 column TaskUID to tableName TaskUID column? Also, when setting tableName can you use the actual name of the table rather than the index to be clear which object you are working with? Commented Feb 10, 2018 at 19:35
  • And are they in the same sheet? Commented Feb 10, 2018 at 19:36
  • no they are in different sheets. however if i replace the tableName with actual table name it works. i am just looking to see how i can use local variables Commented Feb 11, 2018 at 10:40
  • See my answer below Commented Feb 11, 2018 at 10:41

2 Answers 2

2

The reason it does not work is because using the brackets, [], is like calling Application.Evaluate() on the expression inside the brackets, so you cannot use your local variable names in there because the thing that will be evaluating the expression has no idea about them. It cannot find a list object named tableName.

You can avoid the brackets (arguably, you always should):

Dim tableName As ListObject
Set tableName = Worksheets("DataSource").ListObjects(1)

ListObjects("Table1").ListColumns("TaskUID").DataBodyRange.Copy tableName.ListColumns("TaskUID").DataBodyRange
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the answer, seems good, however when i use listObjects i get an error, Sub or Function not defined when i run. Any idea how to solve it
@PrashantBalasubramanyan Apparently you are calling it from a regular module. Then put Worksheets("sheet containing table1"). in front of it.
2

With actual table names:

You can wrap with Range and use the actual table names. You are effectively calling the .Copy method on a named range. So if they were in the same sheet for example:

With ActiveSheet

    .Range("Table1[TaskUID]").Copy .Range("Table2[TaskUID]")

End With

You can always qualify the destination with the sheet name if it is in another sheet.

Or (by column number of table variable):

As you need a range to be used for the copy destination, if you know the column number of the table variable to paste to, you can also write, using the table variable name something like:

.Range("Table1[TaskUID]").Copy  tableName.DataBodyRange.Columns(2) 'or which ever column

Or (by column name of table variable):

.Range("Table1[TaskUID]").Copy tableName.ListColumns("TaskUID").DataBodyRange

You could place tableName.ListColumns("TaskUID").DataBodyRange in its own variable and then refer to that e.g.

Dim rng As Range
Set rng = tableName.ListColumns("TaskUID").DataBodyRange

.Range("Table1[TaskUID]").Copy rng

Edit: As GSerg points out you can't use the variable name inside. "Range will not be able to find a table named tableName because that is the name of a variable, not of a table"

4 Comments

In your second example, sheetName.Range("tableName[TaskUID]") won't work for the same reason as the original OP's code - Range will not be able to find a table named tableName because that is the name of a variable, not of a table. Your first example will work, but only because you are using hard-coded table names Table1 and Table2 instead of the variable name, at which point it would work as [Table1[TaskUID]].Copy [Table2[TaskUID]] too.
@GSerg Good catch.
Thanks a lot of the various scenario, it really help me to get to the final query
No worries. It taught me something when arriving at the solution :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.