vba Sheets.Select 和 Sheets.Activate 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15919455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Sheets.Select and Sheets.Activate?
提问by Mehper C. Palavuzlar
In VBA for Excel, what is the difference between Sheets.Select
and Sheets.Activate
?
在 VBA for Excel 中,Sheets.Select
和之间有什么区别Sheets.Activate
?
回答by chris neilsen
The difference is in their flexibility.
不同之处在于它们的灵活性。
Activate
make the specified sheet the active sheet, and may only be applied to a single sheet
Activate
使指定的工作表成为活动工作表,并且只能应用于单个工作表
Select
allow for optionally extendingthe currently selected sheets to include the specified sheet, eg
Select
允许选择性地扩展当前选定的工作表以包括指定的工作表,例如
Worksheets("Sheet2").Select Replace:=False
and also allow for selecting an array of sheets
并允许选择一组工作表
Sheets(Array("Sheet3", "Sheet2")).Select
In their minimal form Select
and Activate
do the same thing.
在他们最小的形式Select
和Activate
做同样的事情。
For example, if only one sheet is currently selected (say Sheet3
) or if more than one sheet is selected but excludingsay Sheet2
, then Worksheets("Sheet2").Select
and Worksheets("Sheet2").Activate
both make Sheet2
the sole selected and active sheet.
例如,如果当前仅选择了一张纸(例如Sheet3
),或者如果选择了多张纸但不包括say Sheet2
,则Worksheets("Sheet2").Select
和Worksheets("Sheet2").Activate
两者都会使Sheet2
唯一选定的和活动的工作表。
On the other hand, if say both Sheet2
and Sheet3
are selected and Sheet2
is the active sheet, then Worksheets("Sheet3").Activate
leaves both sheets selected and makes Sheet3
the active sheet, whereas Worksheets("Sheet2").Select
makes Sheet3
the sole selected and active sheet.
另一方面,如果说两者Sheet2
和Sheet3
都被选中并且Sheet2
是活动工作表,则Worksheets("Sheet3").Activate
选择两个工作表并使Sheet3
活动工作表,Worksheets("Sheet2").Select
而使Sheet3
唯一选择和活动工作表。
回答by glh
.activate
is you clicking on the worksheet tab.
.activate
您是否单击了工作表选项卡。
.select
simulates you doing a control and click on the tab. In VBA you're not in the sheet yet.
.select
模拟您进行控制并单击选项卡。在 VBA 中,您还没有在工作表中。
You can .select
more than one sheet but .activate
only one.
您可以.select
不止一张纸,但.activate
只能一张。
回答by HarveyFrench
To expand on the above: When the code below is run with Replace:=False no worksheet deactivation event occurs on sheet4. If Replace:=True is used instead then the de-activation event will fire.
扩展上述内容:当下面的代码使用 Replace:=False 运行时,sheet4 上不会发生工作表停用事件。如果使用 Replace:=True 代替,则停用事件将触发。
Preventing the event is desirable in most circumstances as it can cause unexpected behaviour.
在大多数情况下,预防事件是可取的,因为它可能导致意外行为。
This means that select is only the equivalent of CTRL+Clicking a worksheet tab IF replace:=false is used.
这意味着 select 仅相当于 CTRL+Clicking a worksheet tab IF replace:=false 被使用。
sub a
亚
Dim rng As Range
Sheet4.Select Replace:=False
Set rng = Selection
Sheet5.Select Replace:=True
Selection = rng.Value
end sub
结束子
Thanks for your posts as it helped me understand the difference.
感谢您的帖子,因为它帮助我了解了差异。
Harvey
哈维