vba Excel VBA中活动工作簿的设置值

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

Setting value of active workbook in Excel VBA

excelexcel-vbavba

提问by Matt Ridge

I for now have my workbook set up this way, in where the workbook is hard coded into the Set command, I am wondering if there is a way of doing this so I can have it where it is fluid? So no matter where this code is place (in another workbook) the code will adapt to it's new surroundings.

我现在以这种方式设置我的工作簿,其中工作簿被硬编码到 Set 命令中,我想知道是否有办法做到这一点,这样我就可以将它放在流畅的地方?因此,无论此代码位于何处(在另一个工作簿中),代码都会适应它的新环境。

Does this make sense?

这有意义吗?

Below is what I am using now, what I'd like to have it do is for the ActiveWorkbookor something of the sort be used instead. Since this will be set at the beginning of the code and not the middle.

以下是我现在正在使用的内容,我希望它做的是ActiveWorkbook使用某种或类似的东西。因为这将设置在代码的开头而不是中间。

Set wbOOR = Application.Workbooks("Open Order Report.xlsm")

Set wbOOR = Application.Workbooks("Open Order Report.xlsm")

回答by user3357963

You're probably after Set wbOOR = ThisWorkbook

你可能在追求 Set wbOOR = ThisWorkbook

Just to clarify

只是为了澄清

ThisWorkbookwill always refer to the workbook the code resides in

ThisWorkbook将始终引用代码所在的工作簿

ActiveWorkbookwill refer to the workbook that is active

ActiveWorkbook将引用活动的工作簿

Be careful how you use this when dealing with multiple workbooks. It really depends on what you want to achieve as to which is the best option.

在处理多个工作簿时要小心如何使用它。这实际上取决于您想要实现的目标,哪个是最佳选择。

回答by Tim Williams

This is all you need

这就是你所需要的

Set wbOOR = ActiveWorkbook

回答by José

Try this.

尝试这个。

Dim Workbk as workbook
Set Workbk = thisworkbook

Now everything you program will apply just for your containing macro workbook.

现在,您编程的所有内容都将仅适用于包含的宏工作簿。