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
Setting value of active workbook in Excel VBA
提问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 ActiveWorkbook
or 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
只是为了澄清
ThisWorkbook
will always refer to the workbook the code resides in
ThisWorkbook
将始终引用代码所在的工作簿
ActiveWorkbook
will 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.
现在,您编程的所有内容都将仅适用于包含的宏工作簿。