VBA Code to Break Links

mhoncho

New Member
Joined
May 3, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have written code that takes a workbook and copies each tab of the source file into a new individual workbook, saves it as a new file, closes the workbook and then proceeds to the next tab. The last thing I'm trying to do is add code to break all links to the original file in the new workbooks once they are created. Below is the current code and I'm looking for help to insert a string to perform the break link function before saving and closing the new workbook. Note that I'm not trying to paste values, as I want to retain the mathematical formulas, just not the links to the original workbook. Below is the current code that I'm using, would appreciate a suggestion and what and where to insert the break link code.

Sheets("LASTNAME").Select
Sheets("LASTNAME").Copy
ChDir "C:\Users\FIRST.LAST\Desktop"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\FIRST.LAST\Desktop\LASTNAME.xlsx", FileFormat:=xlOpenXMLWorkbook _
, CreateBackup:=False
ActiveWindow.Close
Windows("ORIGINAL SOURCE FILE.xlsm").Activate
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hello. Try next code:
VBA Code:
    Dim i           As Long
    Dim nmName      As Name

    With Sheets("LASTNAME")
        .Copy
    End With

    ChDir "C:\Users\FIRST.LAST\Desktop"
    ActiveWorkbook.SaveAs Filename:= _
            "C:\Users\FIRST.LAST\Desktop\LASTNAME.xlsx", FileFormat:=xlOpenXMLWorkbook _
            , CreateBackup:=False

    ' Delete All Link to another Workbook
    Dim WorkbookLinks As Variant
    WorkbookLinks = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)

    If IsArray(WorkbookLinks) Then

        For i = LBound(WorkbookLinks) To UBound(WorkbookLinks)
            ActiveWorkbook.BreakLink _
                    Name:=WorkbookLinks(i), _
                    Type:=xlLinkTypeExcelLinks
        Next i

    End If

    ' Delete All Name
    For Each nmName In ActiveWorkbook.Names

        With nmName

            If nmName.Visible Then

                If Not CStr(.Name) Like "*!_FilterDatabase" Then

                    If Not CStr(.Name) Like "*!Print_Area" Then
                        .Delete
                    End If

                End If

            End If

        End With

    Next

    ActiveWindow.Close
    Windows("ORIGINAL SOURCE FILE.xlsm").Activate
 
Upvote 1
Solution

Forum statistics

Threads
1,216,077
Messages
6,128,674
Members
449,463
Latest member
Jojomen56

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