Excel Vba:使用表名称作为变量 [英] Excel Vba: Using table name as a variable

查看:154
本文介绍了Excel Vba:使用表名称作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个表的内容复制到另一个表.在此过程中,我仅复制一些列.抱歉,如果我的问题太基础了,因为我不是Excel VBA的新手

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]]

第3行抛出错误必需对象"

Line 3 is throwing an error "Object Required"

任何人都可以帮助语法

推荐答案

不起作用的原因是因为使用方括号 [] 就像调用 Application.Evaluate()放在方括号内的表达式上,因此您不能在其中使用局部变量名,因为将要评估表达式的东西对此一无所知.它找不到名为 tableName 的列表对象.

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

这篇关于Excel Vba:使用表名称作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆