Open file

H

Harlan

I have almost no experience with VBA, so I need as much help as possible. I
would like to create a macro that runs when a workbook is opened that opens
another workbook. The two workbooks are linked and need to be opened at the
same time for everything to work correctly. I just don't trust everyone else
using the file to remember to alway open both at the same time.
Thanks
 
B

Bob Phillips

Option Explicit

Private Sub Workbook_Open()

Workbooks.Open Filename:= "the other book.xls"

End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code


--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
 
R

Ron de Bruin

Hi Harlan

You can use the workbook open event to run the code
http://www.cpearson.com/excel/events.htm

Then you can open the workbook like this

If bIsBookOpen("test.xls") Then
Set destWB = Workbooks("test.xls")
Else
Set destWB = Workbooks.Open("c:\test.xls")
End If

Copy this function in a normal module

Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function
 
Top