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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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