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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:21:31  来源:igfitidea点击:

What is the difference between Sheets.Select and Sheets.Activate?

excelexcel-vbaspreadsheetvba

提问by Mehper C. Palavuzlar

In VBA for Excel, what is the difference between Sheets.Selectand Sheets.Activate?

在 VBA for Excel 中,Sheets.Select和之间有什么区别Sheets.Activate

回答by chris neilsen

The difference is in their flexibility.

不同之处在于它们的灵活性。

Activatemake the specified sheet the active sheet, and may only be applied to a single sheet

Activate使指定的工作表成为活动工作表,并且只能应用于单个工作表

Selectallow 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 Selectand Activatedo the same thing.

在他们最小的形式SelectActivate做同样的事情。

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").Selectand Worksheets("Sheet2").Activateboth make Sheet2the sole selected and active sheet.

例如,如果当前仅选择了一张纸(例如Sheet3),或者如果选择了多张纸但不包括say Sheet2,则Worksheets("Sheet2").SelectWorksheets("Sheet2").Activate两者都会使Sheet2唯一选定的和活动的工作表。

On the other hand, if say both Sheet2and Sheet3are selected and Sheet2is the active sheet, then Worksheets("Sheet3").Activateleaves both sheets selected and makes Sheet3the active sheet, whereas Worksheets("Sheet2").Selectmakes Sheet3the sole selected and active sheet.

另一方面,如果说两者Sheet2Sheet3都被选中并且Sheet2是活动工作表,则Worksheets("Sheet3").Activate选择两个工作表并使Sheet3活动工作表,Worksheets("Sheet2").Select而使Sheet3唯一选择和活动工作表。

回答by glh

.activateis you clicking on the worksheet tab.

.activate您是否单击了工作表选项卡。

.selectsimulates you doing a control and click on the tab. In VBA you're not in the sheet yet.

.select模拟您进行控制并单击选项卡。在 VBA 中,您还没有在工作表中。

You can .selectmore than one sheet but .activateonly 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

哈维