+ Reply to Thread
Results 1 to 12 of 12

Breaking links when using Worksheet.Copy method

  1. #1
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    6

    Breaking links when using Worksheet.Copy method

    Hello,

    I have a big workbook with formulas on some worksheets that refer to other worksheets.

    Now I want to export (or copy) one single worksheet, to a new workbook using a VB macro. But I need to break the links in the new workbook, as I don't want its data to get updated when I change something in the "mother" work book.

    I used the Worksheet.Copy method. In the code below, "Report" is the sheet I want to export (or copy):

    Sub Export()
    Worksheets("Report").Copy
    End Sub

    My problem is that the new workbook keeps links to the mother workbook. It even keeps the macro. This is not what I want, as the exported single sheet is simply a report.

    Could you please help?

    Many thanks in advance.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,462

    Re: Breaking links when using Worksheet.Copy method

    If that's the code, you just end up with a new workbook, unnamed, unsaved, with a copy of the worksheet.

    I would suggest that you select all the data in the copied worksheet and Paste Special | Values. This will convert everything, replacing formulas with their values and losing the links. To lose the macro(s), you'd need to save the new workbook as a non-macro-enabled workbook with a .xlsx file extension.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    06-14-2013
    Location
    Boston
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Breaking links when using Worksheet.Copy method

    Hi Fakhfour,

    I had a similar situation where I had to export multiple sheets from the same workbook. I altered the code that worked for me so you can drop it into your VBA module. This won't save it anywhere, but simply open it in a new workbook and break all links. Also, the macro doesn't carry over.

    I also attached a simple test file I ran with the code and it worked for me.

    Please Login or Register  to view this content.

    Hope it helps.
    Zarley
    Attached Files Attached Files

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,462

    Re: Breaking links when using Worksheet.Copy method

    Neat. Never done that. need to remember this

    Also, the macro doesn't carry over.
    I suspect there is event code in the worksheet and this WILL get carried across.


    Regards, TMS

  5. #5
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Breaking links when using Worksheet.Copy method

    Quote Originally Posted by Zarley View Post
    ...

    I had a similar situation ...
    Zarley
    Thank you very much. It almost worked perfectly. I say almost, because on the exported file, I still see the button that I had created on the original for export.
    Thank you.

  6. #6
    Registered User
    Join Date
    06-14-2013
    Location
    Boston
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Breaking links when using Worksheet.Copy method

    Hi fakhfour,

    You can insert a 'select and delete' code into the VBA macro once the new sheet is created. In the below code, I used "Button 1" as the name of the button, if yours has a different name, simply change it in the code.

    Please Login or Register  to view this content.
    The final completed code should look something like the following:

    Please Login or Register  to view this content.


    Zarley

  7. #7
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Breaking links when using Worksheet.Copy method

    You really rock!!!! Thanks...
    I have another challenge for you: if I have macros in the "Report" sheet, your code will return #NAME? error, as it looks for the functions in the new file. Do you have a solution for this?

  8. #8
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Breaking links when using Worksheet.Copy method

    You really rock!!!! Thanks...
    I have another challenge for you: if I have macros in the "Report" sheet, your code will return #NAME? error, as it looks for the functions in the new file. Do you have a solution for this?

  9. #9
    Registered User
    Join Date
    06-14-2013
    Location
    Boston
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Breaking links when using Worksheet.Copy method

    Could you upload a sample of the source file you're generating the report from? It will help make it easier to determine the cause and possible solution.

    Thanks,
    Zarley

  10. #10
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Breaking links when using Worksheet.Copy method

    Quote Originally Posted by Zarley View Post
    Could you upload a sample of the source file you're generating the report from? It will help make it easier to determine the cause and possible solution.

    Thanks,
    Zarley
    Dear Zarley,
    I should have said that I have functions not macros. Anyway, here is the excel file with which I still have export problems.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    06-14-2013
    Location
    Boston
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Breaking links when using Worksheet.Copy method

    Hi Fakhfour,

    The user defined function changes things a little. From what I know, you can't copy the sheet to a new workbook without generating that "#NAME" issue as the new worksheet does not have that user defined function. So, to work around this, I created a third sheet in your workbook labeled "Export". This allowed me to do a copy/paste special values from your 'Report' sheet and do the exporting from the new 'Export' sheet in order to avoid having the user defined function.

    I assume the file we're working on here is a sample file, so I also included code to copy not only that data from 'Report', but the formats as well. Just in case your working file had formats you wanted to retain on your final exported report.

    The code is below, and I've uploaded the updated file here. Let me know if this works for you!

    Please Login or Register  to view this content.





    Zarley
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    05-16-2014
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Breaking links when using Worksheet.Copy method

    Quote Originally Posted by Zarley View Post
    Hi Fakhfour,
    ...
    Dear Zarley,
    Thank you again. Your example works fine. I'm going to try it on the real workbook (it's 15MB; I couldn't upload it). You really rule.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Breaking Links
    By JO505 in forum Excel General
    Replies: 2
    Last Post: 04-19-2014, 06:35 PM
  2. Links to Excel Spreadsheet - Save without breaking links
    By Danto in forum Word Formatting & General
    Replies: 0
    Last Post: 05-13-2009, 04:00 AM
  3. Breaking Links
    By thelees in forum Excel General
    Replies: 1
    Last Post: 09-24-2005, 03:05 PM
  4. Breaking Links
    By tojo107 in forum Excel General
    Replies: 2
    Last Post: 07-20-2005, 01:05 AM
  5. [SOLVED] Breaking Links
    By Guilherme Loretti in forum Excel General
    Replies: 1
    Last Post: 03-09-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1