6

我有一个电子表格,在 VBA 中它会打开其他电子表格并暂时将它们设置为活动的worksheet.

但是,我有一个循环,在第一次迭代结束时,我需要将活动工作表设置为启动 VBA 模块的原始工作表。我无法设置新Workbook对象来打开原件,因为原件仍在后台打开并且它说它已经打开。

我的问题是我需要将活动更改Workbook为原始活动,而我从来没有工作簿对象来引用它???

'Original workbook is active implicitly

'loop begins

'change active workbook to something else


'Need to change back to original workbook here- but don't have a VBA workbook object

'end of loop
4

1 回答 1

19

使用ThisWorkbookwhich 将引用包含代码的原始工作簿。

或者在代码开始时

Dim Wb As Workbook
Set Wb = ActiveWorkbook

在返回之前激活所有打开的书籍的示例代码ThisWorkbook

Sub Test()
Dim Wb As Workbook
Dim Wb2 As Workbook
Set Wb = ThisWorkbook
For Each Wb2 In Application.Workbooks
    Wb2.Activate
Next
Wb.Activate
End Sub
于 2012-09-12T11:15:29.713 回答