VBA Excel - 我可以在 ActiveSheet 中使用哪些方法/属性

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

VBA Excel - What methods/attributes can I use with ActiveSheet

excelvbaexcel-vbaworksheet

提问by JSM

I have looked at the MSDN page, and also found this questionhelpful, but I would like to know exactly how ActiveSheet behaves. Is it like a Worksheet object? It sound like it just returns or references a Worksheet object. Do normal Worksheet methods and properties work with it?

我查看了MSDN 页面,也发现这个问题很有帮助,但我想确切地知道 ActiveSheet 的行为方式。它像一个工作表对象吗?听起来它只是返回或引用一个 Worksheet 对象。正常的工作表方法和属性可以使用它吗?

I have used it in code I copied from other sources, but I would like to understand what is happening under the hood.

我在从其他来源复制的代码中使用了它,但我想了解幕后发生的事情。

Thanks

谢谢

Bonus question: If I have a control on sheet2, then set active sheet to sheet1 in a userform, can I then set it back to sheet2 when closing the userform? In essence, can I change the sheet below my form to display/manipulate data while the form is active?

额外问题:如果我在 sheet2 上有一个控件,然后在用户窗体中将活动工作表设置为 sheet1,那么在关闭用户窗体时我可以将其设置回 sheet2 吗?本质上,我可以更改表单下方的工作表以在表单处于活动状态时显示/操作数据吗?

回答by Siddharth Rout

Is it like a Worksheet object?

它像一个工作表对象吗?

Yes ActiveSheetis "Like" a worksheet object but they are not the same. ActiveSheetcan be a "Worksheet", "Chart Sheet", "MS Excel 4.0 Macro Sheet" or "MS Excel 5.0 Dialog Sheet"

是的ActiveSheet,“像”一个工作表对象,但它们不一样。ActiveSheet可以是“工作表”、“图表表”、“ MS Excel 4.0 宏表”或“ MS Excel 5.0 对话框表

And hence one should always avoid using Activesheetwhile working with worksheets. You may not be working with the sheet you think you are.

因此,Activesheet在处理工作表时应始终避免使用。您可能没有使用您认为的工作表。

Do normal Worksheet methods and properties work with it?

正常的工作表方法和属性可以使用它吗?

If the ActiveSheetis a Worksheetthen yes. For example, the below will work for a worksheet but not for "MS Excel 5.0 Dialog Sheet"

如果ActiveSheet是,Worksheet则是。例如,以下内容适用于工作表,但不适用于“ MS Excel 5.0 Dialog Sheet

Debug.Print ActiveSheet.Range("A1").Address

Regarding your bonus question, yes you can set any Worksheetto active sheet, i.e. bring it to the front, provided that the Worksheetis not hidden. Else you will have to unhide it first and then activate it.

关于您的奖金问题,是的,您可以将任何设置Worksheet为活动表,即将其放在最前面,前提Worksheet是未隐藏。否则,您必须先取消隐藏它,然后再激活它。

To make a worksheet active, you may use this

要使工作表处于活动状态,您可以使用此

ThisWorkbook.Sheets("Sheet2").Activate

In a nutshell, avoid using ActiveSheet. Work with objects instead. INTERESTING READ

简而言之,避免使用ActiveSheet. 而是使用对象。有趣的阅​​读