Copy Worksheet Save & break links

pignchick

New Member
Joined
Sep 24, 2014
Messages
10
I am looking for some vba code that will allow me to break all the links in a worksheet and then save the file in the same folder/file path but add Excel to the end of the file path.

For example; If I have an excel worksheet named test.xlsm with 20 tabs, I want to save a copy of that entire worksheet in the same folder as test.xlsm, but rename the worksheet as test excel.xls, and break the links in all 20 tabs.

The purpose of this exercise is to archive data, in case the links get messed up.

Any ideas?
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try:

Code:
Sub BreakLinks()
Dim wb As Workbook

Set wb = Application.ActiveWorkbook

  If Not IsEmpty(wb.LinkSources(xlExcelLinks)) Then
    For Each link In wb.LinkSources(xlExcelLinks)
        wb.BreakLink link, xlLinkTypeExcelLinks
    Next link
  End If
End Sub


Tim
 
Upvote 0
That code answers part of my question. However, I still need to save this version with links removed as another file in the same folder. What coding should I add to the code already provided?
 
Upvote 0
Try:

Code:
Sub BreakLinksandSave()
Dim wb As Workbook, fName as string

Set wb = Application.ActiveWorkbook

  If Not IsEmpty(wb.LinkSources(xlExcelLinks)) Then
    For Each link In wb.LinkSources(xlExcelLinks)
        wb.BreakLink link, xlLinkTypeExcelLinks
    Next link
  End If

  fname = left(activeworkbook.name,len(activeworkbook.Name)-5) & "-EXCEL" & right(activeworkbook.Name,5)

  activeworkbook.saveas fname
End Sub


Tim
 
Upvote 0
Thanks Tim, that code worked very well. Only 1 thing, how do I choose the file path? Right now it's saving the copy to my documents. I would like to put it on a shared drive, and in the same folder as the source file.
 
Upvote 0
Replace activeworkbook.name with activeworkbook.fullname (all three instances). That will save the file to the same folder as the source.

Tim
 
Upvote 0
Also, what would be the correct code to keep the original and not the version with the excel ending open after the macro is run? The version without links is mainly for archive purposes.
 
Upvote 0
Here you go:

Code:
Sub BreakLinksandSave()
  Dim link As Variant, wb As Workbook, origFile As String, newFile As String
  
  Set wb = Application.ActiveWorkbook
  
  If Not IsEmpty(wb.LinkSources(xlExcelLinks)) Then
    For Each link In wb.LinkSources(xlExcelLinks)
      wb.BreakLink link, xlLinkTypeExcelLinks
    Next link
  End If
  
  origFile = ActiveWorkbook.FullName
  
  newFile = Left(origFile, Len(origFile) - 5) & "-EXCEL" & Right(origFile, 5)
  
  ActiveWorkbook.SaveAs newFile
  newFile = ActiveWorkbook.Name
  
  Workbooks.Open (origFile)
  Workbooks(newFile).Close False
End Sub


Tim
 
Upvote 0
Something must be slightly off in the logic. When I run the new code I get the newFile to remain open without the links. I was hoping that the origFile would be open at the end with links still in the file and that the newFile would be closed with links broken.
 
Upvote 0
I've improved the code a little, removing the Update Links? message when opening the original file. In my testing, everything seems to work fine - original file name and path is stored, file is renamed, original file is opened, renamed file is closed.

To test on your end, step through the code with the <F8> key.


Code:
Sub BreakLinksandSave()
  Dim link As Variant, wb As Workbook, origFile As String, newFile As String
  
  Set wb = Application.ActiveWorkbook
  
  If Not IsEmpty(wb.LinkSources(xlExcelLinks)) Then
    For Each link In wb.LinkSources(xlExcelLinks)
      wb.BreakLink link, xlLinkTypeExcelLinks
    Next link
  End If
  
  origFile = wb.FullName
  
  newFile = Left(origFile, Len(origFile) - 5) & "-EXCEL" & Right(origFile, 5)
  
  ActiveWorkbook.SaveAs newFile
  newFile = ActiveWorkbook.Name
  
  Application.DisplayAlerts = False
  Workbooks.Open origFile, UpdateLinks:=True
  Application.DisplayAlerts = True
  
  Workbooks(newFile).Close False
End Sub


Tim
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,559
Latest member
MrPJ_Harper

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