如何激活属于不同工作簿的Excel工作表

让我们以这个代码为例(在一个Excel VBAmacros中):

--more code-- Sheets("Sheet2").Activate ActiveSheet.Range("A1").Select --more code-- 

上面的代码行切换到sheet2(我正在使用的工作簿内的一个工作表)。 但是如果我想打开一张另一张工作簿呢?

我尝试过这样的事情:

 Sheets("C:\MYROUTE\[MyWorkbook.xlsx]Sheet2").Activate 

要么

 Sheets("C:\MYROUTE\MyWorkbook.xlsx!Sheet2").Activate 

但它不起作用任何想法,写得好吗?

您可以使用以下代码select工作簿,然后像在活动工作簿中一样使用工作表。

 Dim my_path as String, my_file as String, current_wb as String my_path = "C:\whatever..." my_file = "whatever.xlsx" current_wb = ActiveWorkbook.Name Workbooks.Open my_path & "\" & my_file 'This is to pen the workbook Workbooks(my_file).Activate 'This is to activate the recently opened workbook Workbooks(current_wb).Activate 'This is to activate the workbook that was first open 

将工作簿应用于工作表:

 my_file.Sheets("Sheet2").Range("A1").Select current_wb.Sheets("Sheet1").Range("B2").Select