使用数组引用名称属性 [英] Referencing Name Property Using An Array
问题描述
我正在处理一个项目,需要我根据数组中的位置值来选择工作表。我已经填充了 desArr()
(类型为String),值为 desArr(0)
包含工作表的名称我想参考为了避免用户更改选项卡名称并弄乱代码的问题,我试图引用工作表的CodeName。
I am working on a project that requires me to select worksheets depending on the value of a position in an array. I have populated desArr()
(of type String) with values and desArr(0)
contains the name of the sheet I want to reference. In order to avoid the problem with users changing the tab names and messing up the code, I am trying to reference the CodeName of the worksheet.
通常,我可以这样做:
ThisWorkbook.Worksheets(Import.Name)
其中导入
在VB Editor中的(Name)
属性下指定。但是,我现在想根据 desArr(0)
的值在输入中可变地引用该名称。我试着做以下但是没有用:
where Import
is specified under the (Name)
property in VB Editor. However, I would now like to reference that name variably, as in input depending on the value of desArr(0)
. I tried doing the following but to no avail:
ThisWorkbook.Worksheets(desArr(0).Name)
ThisWorkbook.Worksheets(desArr(0) & ".Name")
有没有人有有什么想法如何解决这个问题?
Does anyone have any idea on how to solve this problem?
推荐答案
只需使用:
Import
这是引用表单对象而不需要知道其名称
属性。只需使用其变量/对象名称。以上相当于,但是比
That's the way to reference a sheet object without needing to know its Name
property. Just use its variable/object name. The above is equivalent to, but much less convoluted than,
ThisWorkbook.Worksheets(Import.Name)
<财产不属于财产。它只是对象变量的名称。从我可以从你的问题推断出来,你的工作表对象称为导入
,所以只需使用它。
编辑:
很难理解你的问题;澄清一点:
It's hard to understand what you are asking; to clarify a bit:
如果我理解正确,你有一张名为导入的表格,你想要防范这种情况:
If I understand correctly, you have a sheet called "Import", and you want to be able to guard against this scenario:
好的,有些用户决定重命名表格是个好主意。请注意,这会将表单对象的名称
属性的值更改为用户输入垃圾
。 名称
属性和标签中写的东西总是相同的;他们是一回事它们被一一对联。
Ok, some user decided it was a good idea to rename the sheet. Note that this changed the sheet object's Name
property's value to "User-input crap"
. The Name
property and the thing written in the tab are always the same; they're the same thing; they're linked one-to-one.
但是,表单对象本身仍然被称为导入
,或无论以前是什么(默认情况下,它是 Sheet1
,但也许您在属性窗口中更改了它。)请参阅:
However, the sheet object itself is still called Import
, or whatever it was earlier. (By default it is Sheet1
, but maybe you changed that in the Properties window.) See:
无论用户输入垃圾,我还是可以说,例如:
Regardless of user-input crap, I can still say, for instance:
Import.Cells(1, 1).Value = "frog"
我也可以这样说,这是无用的复杂但完全相同的:
I can also say this, which is uselessly complicated but perfectly equivalent:
ThisWorkbook.Worksheets(Import.Name).Cells(1, 1).Value = "frog"
I 可以这样说:
I could also say this:
ThisWorkbook.Worksheets("User-input crap").Cells(1, 1).Value = "frog"
但是我必须 用户在表单选项卡中键入的内容,我不知道。
but for that I'd have to know what the user typed in the sheet tab, which I don't.
这篇关于使用数组引用名称属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!