我有一个搜索引擎,它来自两个不同的工作簿,一个是搜索引擎所在的工作簿,所以这很容易,但另一个是分开的。
我想知道是否有一种方法可以链接它们,以便如果打开一个,另一个会。这些将放在维基上,我需要下载它们,有没有办法将它们放在同一个文件中但同时保持它们分开?
我猜答案是否定的,但选择你的大脑并没有什么坏处;)
最实用的方法是将代码放在每个文件中
- check if the other file eas already open
- if not then either
- open the other file (assuming the same location) if not already open
- report the file as missing
为此,您需要ThisWorkbook
在两个工作簿的模块中放置如下代码。
请注意,代码禁用事件以避免新打开的文件尝试重新打开初始文件(尽管代码仍将处理此问题
在第二本书中使用相同的代码,但将strFil
变量更新为另一本书的名称
File#2
Private Sub Workbook_Open()
'This File is File02.xlsm
'Other file is File01.xlsm
Dim strFile1 As String
Dim wb As Workbook
strFile = "File01.xlsm"
On Error Resume Next
Set wb = Workbooks(strFile)
On Error GoTo 0
'if file is already open then leave sun
If wb Is Nothing Then
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
On Error Resume Next
Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & strFile)
On Error GoTo 0
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End If
'file not present
If wb Is Nothing Then MsgBox strFile & " not present", vbCritical
End Sub