2012-07-27 79 views
0

我有一个搜索引擎,它从两个不同的工作簿中提取,一个是搜索引擎,这样很容易,但另一个是分开的。将两个工作簿保存到一个文件中?

我想知道是否有方法将它们连接起来,如果一个被打开,另一个会。这些将被放在维基上,我需要它们都被下载,有没有办法将它们放到同一个文件中,但同时保持它们分开?

我猜的答案是否定的,但它不伤害来接你的大脑;)

回答

0

最实用的方法将被放置代码每个文件

- 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 
的名字
相关问题