Macro to open another workbook

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have a workbook called "Sales by Dept "and when I open this , I would like another workbook called "Intercompany Sales.xls" in C:\my documents to open automatically



See my code below which I have saved into "This Workbok" module but the file "Intercompany Sales.xls" does not automatically open


Code:
Private Sub Workbook_Open()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "C:\My Documents\" 'Change folder location
MyFile = Dir(MyFolder & "\*intercompany*.xls")
Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile
MyFile = Dir
Loop
End Sub


It would be appreciated if someone could amen my code accordingly
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
UNTESTED

VBA Code:
Private Sub Workbook_Open()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "C:\My Documents" 'Change folder location
MyFile = "Intercompany Sales.xls"
Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile
MyFile = Dir
Loop
End Sub
 
Upvote 0
Thnks for the reply

When opening the file , I get invalid procedure or call rguments and the code below is highlighted

Code:
 MyFile = Dir


Kindly advise how to correct this
 
Upvote 0
I did coment it out earlier, but then file goes into a loop and I have to shut it down
 
Upvote 0
.xls is not part of the actual file name
VBA Code:
Private Sub Workbook_Open()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "C:\My Documents" 'Change folder location
MyFile = "Intercompany Sales"
Workbooks.Open Filename:=MyFolder & "\" & MyFile
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,284
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top