解决此问题的最佳方法是在与您要打开的文件相同的文件夹中创建一个名为“launcher.xlsm”的 Excel。在“启动器”文件中,将以下代码放入“工作簿”对象中,但将常量设置为TargetWBName
您希望打开的文件的名称。
Private Const TargetWBName As String = "myworkbook.xlsx"
'// First, a function to tell us if the workbook is already open...
Function WorkbookOpen(WorkBookName As String) As Boolean
' returns TRUE if the workbook is open
WorkbookOpen = False
On Error GoTo WorkBookNotOpen
If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
WorkbookOpen = True
Exit Function
End If
WorkBookNotOpen:
End Function
Private Sub Workbook_Open()
'Check if our target workbook is open
If WorkbookOpen(TargetWBName) = False Then
'set calculation to manual
Application.Calculation = xlCalculationManual
Workbooks.Open ThisWorkbook.Path & "\" & TargetWBName
DoEvents
Me.Close False
End If
End Sub
将常量“TargetWBName”设置为您希望打开的工作簿的名称。此代码将简单地将计算切换为手动,然后打开文件。然后启动器文件将自动关闭。*注意:如果您不希望每次打开此文件时都提示“启用内容”(取决于您的安全设置),您应暂时删除“me.close”以防止其自行关闭,保存文件并将其设置为受信任,然后在再次保存之前重新启用“me.close”调用。或者,您可以将 False 设置为 True
afterMe.Close