Can I break a link, using a VBA

russelldt

Board Regular
Joined
Feb 27, 2021
Messages
160
Office Version
  1. 365
Platform
  1. MacOS
I have a master excel file, that populates data (links) to excel files, when they are opened. I want this to be a once off link, so that when I save the new excel file for the first time, all links are removed, just the raw data remains. I know there is a manual option to break the link, but I am hoping for a fail safe automatic solution.

Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You can use code like this:

VBA Code:
Sub BreakLinks()
    Dim LinkArray
    Dim i                As Long
    LinkArray = ActiveWorkbook.LinkSources(xlExcelLinks)
    If Not IsEmpty(LinkArray) Then
        For i = LBound(LinkArray) To UBound(LinkArray)
            ActiveWorkbook.BreakLink LinkArray(i), xlLinkTypeExcelLinks
        Next i
    End If
End Sub
 
Upvote 0
You can use code like this:

VBA Code:
Sub BreakLinks()
    Dim LinkArray
    Dim i                As Long
    LinkArray = ActiveWorkbook.LinkSources(xlExcelLinks)
    If Not IsEmpty(LinkArray) Then
        For i = LBound(LinkArray) To UBound(LinkArray)
            ActiveWorkbook.BreakLink LinkArray(i), xlLinkTypeExcelLinks
        Next i
    End If
End Sub
Thanks for the prompt response, Rory.
This works great as a Macro, but I have to create a Button and run the Macro for it to work.

Is it possible to run this automatically when I Save and exit the workbook?

Thanks
 
Upvote 0
Call it from the Workbook_BeforeSave event.
 
Upvote 0
Thank you
I need to re think this, apologies.,

I want the links to break when I open the workbook, not when I close it. I have addd the Workbook_Open name, but this is not working.

Private Sub Workbook_Open()
BreakLinks
End Sub

Sub BreakLinks()
Dim LinkArray
Dim i As Long

LinkArray = ActiveWorkbook.LinkSources(xlExcelLinks)

If Not IsEmpty(LinkArray) Then
For i = LBound(LinkArray) To UBound(LinkArray)
ActiveWorkbook.BreakLink LinkArray(i), xlLinkTypeExcelLinks
Next i
End If
End Sub
 
Upvote 0
Replace activeworkbook with thisworkbook
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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