这是我面临的情况。 已经打开了3个工作簿,每个工作簿的名称是book1.xlsx,book2.xlsx,book3.xlsx
我试图选择" book3.xlsx"。
的对象 Dim ExcelApp
Dim objWorkBook, objSheets
Dim args, param
Dim result
Set ExcelApp = GetObject(,"Excel.Application")
Set objWorkBook = ExcelApp.ActiveWorkbook
Wscript.Echo(objWorkBook.name)
预期的结果是book3.xlsx,但它显示了book1.xlsx。 在多个打开的工作簿中可以选择特定的Excel工作簿吗?
答案 0 :(得分:0)
如果要遍历不同的工作簿,请使用以下内容:
Dim ExcelApp
Dim objWorkbook, objSheets
Dim args, param
Dim result
Set ExcelApp = GetObject(,"Excel.Application")
For Each objWorkbook in ExcelApp.Workbooks
Wscript.Echo(objWorkBook.Name)
If objWorkbook.Name = "book3.xlsx" Then
Exit For ' This is the workbook we wanted
End If
Next
If objWorkbook.Name = "book3.xlsx" Then
'objWorkbook is set to book3, so do whatever you like
Else
' No book3 found, throw up a message box to warn the user
MsgBox "No Book3.xlsx file found"
End If
如果你只需要直接选择工作簿(而且你确定它已经存在):
Dim ExcelApp
Dim objWorkbook, objSheets
Dim args, param
Dim result
Set ExcelApp = GetObject(,"Excel.Application")
Set objWorkbook = ExcelApp.Workbooks("book3.xlsx")
'objWorkbook is set to book3, so do whatever you like with it