我有一些代码可以打开多个受密码保护的工作簿。在代码末尾,我试图重新激活主工作簿中的工作表。但是,它不会重新激活。可能是由于打开过程需要花费更长的时间来运行子程序?有什么建议吗?
我尝试关闭事件。还尝试了等待功能。
Public Sub OpenFiles()
Dim mainwb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim numClients
Dim pw As String
Dim i As Integer
Dim newHour As Variant
Dim newMinute As Variant
Dim newSecond As Variant
Dim waitTime As Variant
Set mainwb = ThisWorkbook
myPath = mainwb.Sheets("Dashboard").Range("C1")
myExtension = ".xlsx"
numClients = mainwb.Sheets("Dashboard").Range("I1")
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
For i = 4 To numClients
myFile = ThisWorkbook.Sheets("Dashboard").Range("A" & i).Value
pw = ThisWorkbook.Sheets("Dashboard").Range("B" & i).Value
Workbooks.Open Filename:=myPath & myFile & myExtension, Password:=pw
DoEvents
Next i
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
ThisWorkbook.Worksheets("Dashboard").Activate
End Sub
激活的工作表是最后打开的工作簿上的工作表。
答案 0 :(得分:0)
我修改了您的代码以激活特定的工作簿,但是您需要输入工作簿名称。
Dim mainwb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim numClients
Dim pw As String
Dim i As Integer
Dim newHour As Variant
Dim newMinute As Variant
Dim newSecond As Variant
Dim waitTime As Variant
Set mainwb = ThisWorkbook
myPath = mainwb.Sheets("Dashboard").Range("C1")
myExtension = ".xlsx"
numClients = mainwb.Sheets("Dashboard").Range("I1")
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
For i = 4 To numClients
myFile = ThisWorkbook.Sheets("Dashboard").Range("A" & i).Value
pw = ThisWorkbook.Sheets("Dashboard").Range("B" & i).Value
Workbooks.Open Filename:=myPath & myFile & myExtension, Password:=pw
DoEvents
Next i
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Workbooks("YourWorkbookName.xlsx").Activate 'Put your workbook name here. can add worksheet as well
End Sub````