Macro to open source workbook then close

drw7162

New Member
Joined
Feb 4, 2022
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
I looked but I couldn't find it. I am new to writing macros so if this has been answered I apologize. I have a workbook that has data that can only be updated by opening another source workbook so what I need to do is just that. When the excel file is opened, it automatically opens the source workbook which would let the original excel file be updated and then close out the source workbook and leave the original file open. I was able to find code that opens the source workbook immediately upon opening the workbook but it stays open and doesn't close. I'm guessing there is s simple line of code I am missing? Thanks for any help.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi drw
Just put the below in the "ThisWorkbook" macro

VBA Code:
Private Sub Workbook_Open()
    Dim wb As Workbook
    Set wb = Application.Workbooks.Add("E:\OneDrive\Documents\Source.xlsx")
    wb.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
    wb.Close True
    
End Sub
 
Upvote 0
Solution
Hi,
Thanks for the response. I put in the macro, and I am now getting this error every time i open the workbook
1644036040186.png

oddly enough it is actually updating the workbook when i make changes to the source but the error still comes up, do you know what the issue is?
 
Upvote 0
If you comment out the below line does it still work
VBA Code:
Private Sub Workbook_Open()
    Dim wb As Workbook
    Set wb = Application.Workbooks.Add("E:\OneDrive\Documents\Source.xlsx")
    'wb.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
    wb.Close True
    
End Sub

The error you are getting is usually when you have broken links to other workbooks or if a linked workbook wasn't calculated before closing
But if omitting the line of code for updating links works use that
If your data relys on those links to also be updated then you'l need to click edit links and fix them
there is a way of turning off these dialog boxes but I don;t like to as it leads to incorrect data if your worksheet relys on ot being correct
 
Upvote 0
If you comment out the below line does it still work
VBA Code:
Private Sub Workbook_Open()
    Dim wb As Workbook
    Set wb = Application.Workbooks.Add("E:\OneDrive\Documents\Source.xlsx")
    'wb.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
    wb.Close True
   
End Sub

The error you are getting is usually when you have broken links to other workbooks or if a linked workbook wasn't calculated before closing
But if omitting the line of code for updating links works use that
If your data relys on those links to also be updated then you'l need to click edit links and fix them
there is a way of turning off these dialog boxes but I don;t like to as it leads to incorrect data if your worksheet relys on ot being correct
I get it to work once without errors then the next time it's opened same error... don't know if it's got something to do with network drive...
 
Upvote 0
you can try this but be careful to make sure its back on if something errors out
Code:
=vbaPrivate Sub Workbook_Open()
    Dim wb As Workbook
    Application.DisplayAlerts = False
    Set wb = Application.Workbooks.Add("E:\OneDrive\Documents\Source.xlsx")
    'wb.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
    wb.Close True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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