为什么选择之前必须先激活Excel工作表? [英] Why do Excel sheets have to be activated before selection?
问题描述
此代码
Sheets(1).Activate
Sheets(2).Range("A1").Select
在VBA中将失败,因为您只能在Active
对象上使用Select
.我知道是这种情况.
will fail in VBA because you can only use Select
on an object which is Active
. I understand this is the case.
Excel数据模型的哪个元素导致出现这种情况?我想在使用Select
之前,用户/编码器会隐式地意图Activate
任何对象-我不明白为什么VBA不会做出这种假设,并且,我认为这种区别是有原因的存在.
What element of the Excel datamodel causes this to be the case? I would think there is an implicit intent from a user/coder to Activate
any object immediately prior to using Select
- I do not understand why VBA would not make this assumption, and, I am assuming there is a reason this distinction exists.
- Excel数据模型的哪一部分阻止选择而不激活?
推荐答案
正如brettdj所指出的,不必为了选择范围而激活表格.这是带有大量选择单元格/范围的示例的参考.
As brettdj pointed out, you do not have to activate the sheet in order to select a range. Here's a reference with a surprisingly large amount of examples for selecting cells/ranges.
现在,为什么我必须首先激活工作表?我不认为这是数据模型的错,而仅仅是Ranges的select方法的局限性.
Now as for the why do I have to active the sheet first? I do not believe it is a fault of the datamodel, but simply a limitation of the select method for Ranges.
从实验来看,在Excel中选择一个范围似乎有两个要求.
From experimentation, it looks like there are two requirements to select a range in Excel.
- Excel必须能够更新UI来指示所选内容.
- 父级(即工作表)范围必须处于活动状态.
要支持此声明,您也不能从隐藏的工作表中选择一个单元格.
To support this claim, you also cannot select a cell from a hidden sheet.
Sheets(1).Visible = False
Sheets(1).Activate
'The next line fails because the Range cannot be selected.
Sheets(1).Range("A1").Select
简单地说,当涉及范围时,您不能选择一个看不到的地方.
Simply put, when it comes to Ranges, you cannot select one you cannot see.
除了可以实际选择隐藏工作表中的对象外,我本来是限制全部选择的局限性.愚蠢的Excel.
I would have claimed this is a limitation of select all together, except that you can actually select an object in a hidden sheet. Silly Excel.
这篇关于为什么选择之前必须先激活Excel工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!