这就是我现在所拥有的,
Dim mastertemp As Workbook
Dim testproject As Workbook
Set testproject = Workbooks("C:\Users\zzz\Documents\Test Project.xlsm")
Set mastertemp = Workbooks("C:\Users\zzz\Documents\MasterTemp.xlsx")
mastersheet.sheets("Sheet1").activate
第三行代码给我下标超出范围,任何想法?
我希望能够在工作簿之间跳转而系统没有给我“工作簿已经打开,重新打开会丢弃所有更改等”
答案 0 :(得分:0)
我会做这样的事情:
Dim wbk as Workbook
Set wbk = Workbooks("Test Project.xlsm")
do stuff
Workbooks.Open ("C:\Users\zzz\Documents\MasterTemp.xlsx")
do stuff
wbk.Sheets("Dashboard").Activate
答案 1 :(得分:0)
如果您知道工作簿已经打开,请在Workbooks
集合中按名称引用它:
Dim testProject as Workbook
Set testProject = Workbooks("C:\Users\zzz\Documents\Test Project.xlsm")
testProject.Sheets("Dashboard").Activate
如果您不知道工作簿是否已打开,那么您可以使用一些错误处理逻辑,例如:
Dim testProject as Workbook
On Error Resume Next
' Attempt to index this workbook from the open Workbooks collection:
Set testProject = Workbooks("C:\Users\zzz\Documents\Test Project.xlsm")
If Err.Number <> 0 Then
' If the above fails, then the workbook isn't open, so we need to open it:
Set testProject = Workbooks.Open("C:\Users\zzz\Documents\Test Project.xlsm")
End If
On Error GoTo 0
答案 2 :(得分:0)
如果您始终知道工作簿的名称,则可以
workbooks("Test Project").sheets("Dashboard").Activate 'add the file extension to the name if you've turned on file extensions in windows explorer
如果工作簿名称可能正在更改(如果不是,那么晚上,并且您将多次引用该工作簿),那么查找窗口的建议就是可行的方法。
dim TestWorkbook as workbook
set TestWorkbook=Workbooks.Open ("C:\Users\zzz\Documents\Test Project.xlsm")
TestWorkbook.sheets("Dashboard").activate
我希望这会有所帮助。