简体   繁体   English

Excel VBA:打开新工作簿时ActiveWorkbook不会更改

[英]Excel VBA: ActiveWorkbook not changing when I open a new Workbook

Here is the small bit of code that should play a role: 以下是应发挥作用的少量代码:

Workbooks.Open (MPPGLoc)

ActiveWorkbook.Sheets("MPPG").Activate

ActiveSheet.Move Before:=Workbooks("DPV Acq Test.xlsm").Sheets(1)

I am having the user input the file location for a payment plan, then supposedly going to the correct sheet in the plan and trying to copy it into a different workbook which automates everything. 我让用户输入付款计划的文件位置,然后假定要转到计划中的正确工作表,然后尝试将其复制到另一个可以自动完成所有工作的工作簿中。 However this keeps simply moving one of the worksheets in the original workbook in front of the other worksheets. 但是,这只是将原始工作簿中的一个工作表移到其他工作表的前面。

Not sure if the problem is because opening a workbook doesn't necessarily make it active, or a problem with my move function. 不知道问题是因为打开工作簿不一定使它处于活动状态,还是我的移动功能出现了问题。 Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

And hence you should avoid using Activeworkbook Use Objects. 因此,您应该避免使用Activeworkbook Use Objects。

See this example ( Untested ) 看到这个例子( 未经测试

Dim wb As worksbook, wbNew As Workbook
Dim wsNew As Worksheet

'~~> I am assuming that you are running the code from
'~~> Workbooks("DPV Acq Test.xlsm") 
Set wb = ThisWorkbook

Set wbNew = Workbooks.Open(MPPGLoc)
Set wsNew = wbNew.Sheets("MPPG")

wsNew.Move Before:=wb.Sheets(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM