Excel VBA to Break Links – 4 Examples

 

The following gif provides an overview.

excel vba break links


The following dataset contains the sales overview for two months. B5:C11 is linked to a different workbook:  Sales January.

Dataset for Excel VBA to Break Links

D5:E11 is linked to another workbook: Sales February.

Cell Range Linked to Another Workbook


Example 1 – Use VBA If Statement with For Next Loop to Break Links in the Whole Workbook

Use an If Statement with a For Next Loop in the VBA code.

Steps:

  • Go to the Developer tab.
  • Select Visual Basic.

Opening Visual Basic Editor Window for Writing VBA Code to Break Links in Excel

  • In the Visual Basic Editor window, select Insert.
  • Choose Module.

Opening Module in Visual Basic Editor Window

 

  • Enter the following code in the Module.
Sub Break_Links()
Dim act_wb As Workbook
Set act_wb = Application.ActiveWorkbook
If Not IsEmpty(act_wb.LinkSources(xlExcelLinks)) Then
            For Each ext_link In act_wb.LinkSources(xlExcelLinks)
            act_wb.BreakLink ext_link, xlLinkTypeExcelLinks
            Next ext_link
End If
End Sub

VBA Code with If Statement And For Next Loop to Break Links in Excel

Code Breakdown

  • a Sub Procedure: Break_links is created.
  • a variable: act_wb is declared as Workbook.
  • the Application.ActiveWorkbook Property sets act_wb.
  • The If Statement and  the IsEmpty function check if the variable was initialized.
  • The For Next Loop goes through each link in the workbook.
  • the BreakLink Method  converts the formulas that are linked to other sources to values.
  • the If Statement is ended.
  • the Sub Procedure is ended.
  • Save the code and go back to your worksheet.

Saving VBA Code to Break Links in Excel

  • Go to the Developer tab.
  • Select Macros.

Selecting Macros Command in Excel

  • In the Macro dialog box, select Break_Links as the Macro name.
  • Click Run.

Running VBA Code to Break Links in Excel

 

  • Select a cell and you will see the value only, not the link.

Links are Removed from Values

  • The link is removed.

External Links Are Broken and Only Values Remain

Read More: How to Break Links in Excel When Source Is Not Found


Example 2 – Applying the On Error Statement to Break Links in Excel Ignoring Error Warnings

On Error Statement is used to enable an error-handling routine within a procedure. Use the On Error Resume Next form of the statement to create a VBA code.

Steps:

  • Open a VBA Module by following the steps in Example 1.
  • Enter the following code in the Module.
Sub remove_links()
Dim ext_links As Variant
Dim j As Integer
ext_links = ActiveWorkbook.LinkSources(1)
On Error Resume Next
For j = 1 To UBound(ext_links)
ActiveWorkbook.BreakLink ext_links(j), xlLinkTypeExcelLinks
Next j
On Error GoTo 0
End Sub

VBA Code with On Error Statement to Break Links in Excel

 Code Breakdown

  •  a Sub Procedure:remove_links is created.
  •  a variable: ext_links is declared as a Variant and another variable: j as an Integer.
  • the ActiveWorkbook.LinkSources returns an array of links present in the active workbook.
  • the On Error Resume Next enables an error-handling routine.
  • a For Next Loop goes through all the links and the BreakLink Method converts the formulas that are linked to other sources to values.
  • the Sub Procedure is ended.
  • Save the code and go back to your worksheet.
  • Go to the Developer tab.
  • Select Macros.

Using Macros Command to Run VBA Code in Excel

  • In the Macro dialog box, select remove_links as the Macro name.
  • Click Run.

Running Macros to Break Links in Excel VBA

  • Select a cell, here C5. You can see the value only, not the link.

Link Has Been Broken and Cell Only Contains Value

Read More: How to Break Links in Excel and Keep Values


Example 3 – Use the LBound and UBound Functions to Find the Size of an Array and to Break Links in Excel

The LBound function is used to find the lower limit of an array dimension and the UBound function is used to find its upper limit.

Steps:

  • Open a VBA Module by following the steps in Example 1.
  • Enter the following code in the Module.
Sub brk_links()
Dim ext_links As Variant
Dim p As Integer
            ext_links = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
            For p = LBound(ext_links) To UBound(ext_links)
            ActiveWorkbook.BreakLink _
            Name:=ext_links(p), _
            Type:=xlLinkTypeExcelLinks
            Next p
End Sub

VBA Code with LBound and UBound Functions to Break Links in Excel

Code Breakdown

  •  a Sub Procedure: brk_links is created.
  •  a variable: ext_links is declared as a Variant and another variable: p as an Integer.
  • the ActiveWorkbook.LinkSources returns an array of links present in the active workbook.
  • in the For Next Loop, the LBound and UBound functions get the size of the array
  • the BreakLink Method converts the formulas that are linked to other sources to values.
  • the Sub Procedure is ended.
  • Save the code and go back to your worksheet.
  • Go to the Developer tab.
  • Select Macros.

Employing Macros Feature

  • In the Macro dialog box, select brk_links as the Macro name.
  • Click Run.

Using Macro Dialog Box to Break Links in Excel VBA

  • Links were removed.

Only Values Remain in Cells


Example 4 – Display the Count of Broken Links in MsgBox in Excel

Steps:

  • Press Alt + F11 to open the Visual Basic Editor.

Using Keyboard Shortcut to Open VBA Editor

  • Select the Insert tab.
  • Choose Module.

Opening Module for Writing VBA Code to Break Links in Excel

 

  • Enter the following code in the Module.
Sub break_ext_links()
Dim ext_links As Variant
Dim y As Long
Dim brk_count As Long
ext_links = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
If IsEmpty(ext_links) = True Then GoTo ReportResults
For y = 1 To UBound(ext_links)
ActiveWorkbook.BreakLink Name:=ext_links(y), Type:=xlLinkTypeExcelLinks
brk_count = brk_count + 1
Next y
ReportResults:
MsgBox "No of Broken Links: " & brk_count
End Sub

VBA Code for Displaying Number of Broken Links in MsgBox

Code Breakdown

  •  a Sub Procedure: break_ext_links is created.
  • a variable: ext_links is declared as a Variant, y as a Long, and brk_count as Long.
  • the ActiveWorkbook.LinkSources returns an array of links present in the active workbook.
  • the If Statement, and the IsEmpty function check if the variable was initialized.
  • a For Next Loop goes through each link in the workbook.
  • the BreakLink Method converts the formulas that are linked to other sources to values.
  • the MsgBox function displays the number of broken links.
  • the Sub Procedure is ended.
  • Save the code and go back to your worksheet.
  • Press Alt + F8 to open the Macro dialog box.

Applying Keyboard Shortcut to Open Macro Dialog Box

 

  • Select break_ext_links as the Macro name.
  • Click Run.

Employing Macro Dialog Box to Run Code

  • A MsgBox will be displayed with the No of Broken Links.
  • Click OK.

Output MsgBox from VBA Code

  • Links were removed.

Final Output with Broken Links


Things to Remember

  • Whenever working with VBA, you must save the Excel file as Excel Macro-Enabled Workbook.

Practice Section

Practice here.

Practice Sheet to Break Links in Excel VBA


Download Practice Workbook

Download the practice workbook here.


Related Articles

<< Go Back To Excel Break Links | Linking in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mashhura Jahan
Mashhura Jahan

Mashhura Jahan, BSc in Industrial and Production Engineering from Bangladesh University of Engineering and Technology, invested 1.5 years at ExcelDemy. In roles like Excel & VBA Content Developer, Excel Charts, and Dashboard course trainer, she wrote 90+ articles. She was previously part of the forum support team and a junior software analyst on the Excel Add-in project. With interests spanning Excel, VBA, Power Query, Python, Data Science, and Software Development, Mashhura brings a diverse skill set to her... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo