vba Windows().Activate 的超出范围错误

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

Out of range error for Windows().Activate

excelvba

提问by murugan_kotheesan

I have a bit of code in my macro as follows

我的宏中有一些代码如下

ChDir File_pth
Workbooks.Open filename:= File_pth & "\" & open_tkt
Workbooks.Open filename:= File_pth & "\" & closed_tkt
ActiveSheet.Select
Windows("MSS - Ticket Backlog Benchmark_2013 09 05.xlsx").Activate
Sheets("Extract -prev").Select

When I run the macro to open two other files, it should go back to the file in which I run macro to do some operations in the same Xl workbook.

当我运行宏打开另外两个文件时,它应该返回到我运行宏的文件,以便在同一个 Xl 工作簿中执行一些操作。

I get the out of range error in the line

我在行中得到超出范围的错误

Windows("MSS - Ticket Backlog Benchmark_2013 09 05.xlsx").Activate

The file MSS - Ticket Backlog Benchmark_2013 09 05.xlsx exists.

文件 MSS - Ticket Backlog Benchmark_2013 09 05.xlsx 存在。

回答by Our Man in Bananas

try

尝试

windows("MSS - Ticket Backlog Benchmark_2013 09 05").Activate

windows("MSS - Ticket Backlog Benchmark_2013 09 05").Activate

you could check the names of all open workbooks in Debug window like this:

您可以在调试窗口中检查所有打开的工作簿的名称,如下所示:

dim oBook as workbook

for each obook in workbooks
    debug.print ">" & obook.name & "<"
next

回答by NickSlash

Activating the original sheet should bring the original window to the front too, so you shouldn't need to play with the windows. (works for me anyway)

激活原始工作表也应该将原始窗口带到前面,因此您不需要玩弄这些窗口。(无论如何对我有用)

Dim Book As Workbook ' probably not needed
Set Book = ThisWorkbook ' probably not needed
Dim Sheet As Worksheet
Set Sheet = ActiveSheet
ChDir File_pth
Workbooks.Open filename:= File_pth & "\" & open_tkt
Workbooks.Open filename:= File_pth & "\" & closed_tkt
Book.Activate() ' probably not needed
Sheet.Activate()

I've added code to activate the original workbook before activating the original sheet (lines with the comments at the end) and as it says, they are probably not needed.

在激活原始工作表(末尾带有注释的行)之前,我已经添加了激活原始工作簿的代码,正如它所说,它们可能不需要。

The variables Bookand Sheetare references to The active workbook (ThisWorkbook) and the active sheet (ActiveSheet). This is what @mehow was suggesting.

变量BookSheet是对活动工作簿 (ThisWorkbook) 和活动工作表 (ActiveSheet) 的引用。这就是@mehow 的建议。

回答by Eber Colegio

You must place your macros in modules instead of in "This Workbook" or in specific sheets. Otherwise, you will have that window interaction problem. I had the very same issue and that's how I solved it.

您必须将宏放在模块中,而不是放在“本工作簿”或特定工作表中。否则,您将遇到窗口交互问题。我有同样的问题,这就是我解决它的方式。

Like this:

像这样: