简体   繁体   English

如何激活使用VBA中的工作簿名称打开的工作簿

[英]How to activate a workbook that is open using the name of the workbook in VBA

I have already a one workbook open but I am running a macro from another workbook.我已经打开了一个工作簿,但我正在运行另一个工作簿中的宏。 I would like to activate the first workbook using its name.我想用它的名字激活第一个工作簿。

The code:代码:

FileName = input_path_1 & input_file_1
Workbooks(FileName.xls).Activate

When I try to do so, it is giving me "Subscript out of range" error.当我尝试这样做时,出现“下标超出范围”错误。 How do I solve it?我该如何解决?

Check if your variable Filename contains the correct filename.检查您的变量Filename包含正确的文件名。 (eg Sample.xls ) (例如Sample.xls
Also check if input_path_1 and input_file_1 have correct values.还要检查input_path_1input_file_1是否有正确的值。
If they have it should be like this:如果他们有它应该是这样的:

Workbooks(Filename).Activate

Now, if you need to append the extension name (eg Filename value is just Sample ):现在,如果您需要附加扩展名(例如Filename值只是Sample ):

Workbooks(Filename & ".xls").Activate

The argument should always be in the form of string and should be the complete filename (with extension).参数应始终为字符串形式,并且应为完整的文件名(带扩展名)。 Although numerals (index) is also accepted, you can't be sure what index refer to what workbook.尽管也接受数字(索引),但您无法确定哪个索引指的是哪个工作簿。 Better yet, assign it to a variable.更好的是,将它分配给一个变量。

Dim otherWB As Workbook
Set otherWB = Workbooks(Filename)
'Set otherWB = Workbooks(Filename & ".xls") '~~> for second scenario above

Edit1: From comment, if Filename contains the fullpath, then this might work. Edit1:根据评论,如果Filename包含完整路径,那么这可能有效。

Dim Filename1 As String
Filename1 = Split(Filename, "\")(UBound(Split(Filename, "\")))
Workbooks(Filename1).Activate

Only way to access the window of the specific workbook is by below method访问特定工作簿窗口的唯一方法是通过以下方法

Vba VBA

Dim filename as string
set filename = Path.GetFileName(fullFilename)

set Workbook.Windows(filename).WindowState = Excel.XlWindowState.xlMinimized
set Workbook.Windows(filename).WindowState = Excel.XlWindowState.xlNormal

' You can also use Worksheet.Activate() here if you want

C# C#

string filename;
filename = Path.GetFileName(fullFilename);

Workbook.Windows[filename].WindowState = Excel.XlWindowState.xlMinimized;
Workbook.Windows[filename].WindowState = Excel.XlWindowState.xlNormal;

// you can also use Worksheet.Activate() here if you want

Set OutsideWb = Workbooks("path + Filename.xlsm") wont work if workbook already open如果工作簿已打开,则Set OutsideWb = Workbooks("path + Filename.xlsm")不起作用

set a global wb variable to the opened file and use that eg.为打开的文件设置一个全局 wb 变量并使用它,例如。

Set oXLBook = oXLApp.Workbooks.Open("path + Filename.xlsm") '
  Set OutsideWb = oXLBook 'prolly dont need oxlbook  todo

In Excel 2019, Excel 2019年,

Workbooks(Filename).Activate may not work if ".xlsx" is part of the variable name.如果“.xlsx”是变量名称的一部分, Workbooks(Filename).Activate可能不起作用。
Example: Filename = "123_myfile.xlsx" may not activate the workbook.示例:Filename = "123_myfile.xlsx" 可能无法激活工作簿。

In this case, try:在这种情况下,请尝试:

Filename = left(Filename,len(Filename)-5) 'Filename now = "123_myfile" Filename = left(Filename,len(Filename)-5) 'Filename now = "123_myfile"

Workbooks(Filename & ".xlsx").Activate

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

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