vba Excel 选择与激活

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7180008/
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 11:48:57  来源:igfitidea点击:

Excel Select vs Activate

excelvbaexcel-vba

提问by Bruno

What is the difference between the VBA code wb.Sheets(1).Cells.Selectand wb.Sheets(1).Activate?

VBA 代码wb.Sheets(1).Cells.Selectwb.Sheets(1).Activate?

回答by aevanko

Difference between select is that you can select several objects at once. Objects that are selected are also placed in the Selection object which you can use methods on. Unless you are selecting multiple objects, selecting (say, a cell) activates the object.

select 之间的区别在于您可以一次选择多个对象。选择的对象也放置在您可以对其使用方法的 Selection 对象中。除非您选择多个对象,否则选择(例如,一个单元格)会激活该对象。

Activate just simply makes the object the active object. Best way to think of it is "many cells can be selected, but only one may be the active cell at any given time."

Activate 只是简单地使对象成为活动对象。最好的理解方式是“可以选择许多单元格,但在任何给定时间只有一个可能是活动单元格。”

Note: They both have one thing in common - they are rarely ever needed and they do generally don't do anything but slow your code down. You can work directly on an object without selecting or activating it and it's best practice not to use these unless needed.

注意:它们都有一个共同点——它们很少被需要,而且它们通常不会做任何事情,只会减慢你的代码速度。您可以直接处理对象而无需选择或激活它,除非需要,否则最好不要使用这些对象。

回答by Taryn

Here is an explanation from MSDN

这是来自MSDN的解释

You first example wb.Sheets(1).Cells.Selectallows you to select multiple cells

您的第一个示例wb.Sheets(1).Cells.Select允许您选择多个单元格

The second wb.Sheets(1).Activatemakes the sheet active.

第二个wb.Sheets(1).Activate使工作表处于活动状态。

There are lots of resources out there to help with Excel VBA.

有很多资源可以帮助您使用 Excel VBA。

http://www.excel-vba.com/index.htm#Tutorial%20on%20Excel%20Macros

http://www.excel-vba.com/index.htm#Tutorial%20on%20Excel%20Macros

http://www.excel-vba-easy.com/

http://www.excel-vba-easy.com/

http://www.functionx.com/vbaexcel/

http://www.functionx.com/vbaexcel/

回答by Tim Williams

The first selects all cells on the first sheet of the workbook wb. It will fail if the sheet is not active.

第一个选择工作簿 wb 第一个工作表上的所有单元格。如果工作表未处于活动状态,它将失败。

The second just activates the first sheet of the workbook wb. It does not alter the selection or activecell on that sheet, and in some cases there may be no selected range or activecell (eg. if there's an object on the sheet which is currently selected).

第二个只是激活工作簿 wb 的第一张纸。它不会改变该工作表上的选择或活动单元格,并且在某些情况下可能没有选定的范围或活动单元格(例如,如果当前选择的工作表上有一个对象)。

回答by waqasahmed

Select- "Selects" Cell(s)

Select- “选择”单元格

Activate- "Activates" a sheet (kind of like saying to focus on a sheet)

Activate- “激活”一张纸(有点像说专注于一张纸)

Sometimes u need to specifically ACTIVATEthe sheet, in order to make a SELECT

有时你需要专门ACTIVATE的工作表,为了制作一个SELECT

回答by Reverus

I found this question while searching, I had the same question. Here is something I noticed:

我在搜索时发现了这个问题,我有同样的问题。这是我注意到的一点:

 Sub Transfer(x As Long)
   Dim Rng, ID as Range
   Dim i, j, n As Long

 Worksheets(5).Activate
 n = Worksheets(5).Range(Range("I88"), Range("I88").End(xlToRight)).Count

Worksheets(x).Select
 Set Rng = Worksheets(3).UsedRange.Find("Element", LookIn:=xlValues).Offset(1, 1)
 Set ElemID = Range(ElemRng.Offset(0, -1), ElemRng.Offset(0, -1).End(xlDown))
 Set ElemRng = Worksheets(3).Range(ElemRng, ElemRng.End(xlToRight))
End Sub

I found that I HADto put the worksheet.activate (or select) in or the code would run into:

我发现我HAD把worksheet.activate(或选择)或代码将运行到:



Run-time error: '1004' Application-defined or object-defined error

运行时错误:“1004”应用程序定义或对象定义错误



回答by Cornelis

Activate is often used for Sheets for Example. The Active sheet wil be shown on the screen... therfore there can only be one active sheet

例如,Activate 通常用于 Sheets。活动工作表将显示在屏幕上...因此只能有一个活动工作表

Select though is can be used for multiple Cells for Example. Range(A1:B3).Select will select multiple cell which is'nt possible with activate

例如,选择可用于多个单元格。Range(A1:B3).Select 将选择多个无法激活的单元格