+ Reply to Thread
Results 1 to 7 of 7

Export worksheets to new file VBA to 'Break Links' on export?

Hybrid View

  1. #1
    Registered User
    Join Date
    03-01-2019
    Location
    Nottingham, England
    MS-Off Ver
    2016/365
    Posts
    83

    Export worksheets to new file VBA to 'Break Links' on export?

    Hi Guys,

    I'm using this code to export 3 select worksheets in my workbook to a new file which in essence works great.

    Sub ExportNew()
    
    Dim FileName As String
    Dim Path As String
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Path = "C:\This location\"
    FileName = "New File " & Range("A1").Value & ".xlsx"
    
    Sheets(Array("Data", "Pivot", "Mapping")).Copy
    
    With ActiveWorkbook
    .SaveAs Path & FileName, xlOpenXMLWorkbook
    End With
    ActiveWorkbook.Close
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    End Sub
    The problem I'm having now though is that when the sheets are exported, as the 3 sheets refer to eachother (data on one sheet, mapping on the 2nd and pivot on the other), the new file ends up with formula links to the original workbook, which is not ideal.

    Does anyone have any suggestions how I can resolve this issue with VBA or otherwise?

    Thanks,


    Spoonman

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Export worksheets to new file VBA to 'Break Links' on export?

    Sub ExportNew()
        
        Dim FileName  As String
        Dim Path      As String
        Dim ws        As Worksheet
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        Path = "C:\This location\"
        FileName = "New File " & Range("A1").Value & ".xlsx"
        
        Sheets(Array("Data", "Pivot", "Mapping")).Copy
        
        With ActiveWorkbook
            For Each ws In .Worksheets
                ws.UsedRange.Value = ws.UsedRange.Value
            Next ws
        
            .SaveAs Path & FileName, xlOpenXMLWorkbook
            .Close False
        End With
        
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    03-01-2019
    Location
    Nottingham, England
    MS-Off Ver
    2016/365
    Posts
    83
    Thanks AlphaFrog,

    I've tried running the above code and i get an error on
     ws.UsedRange.Value = ws.UsedRange.Value
    Thanks
    Last edited by davesexcel; 02-14-2021 at 03:52 AM.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Export worksheets to new file VBA to 'Break Links' on export?

    What's the error description?

  5. #5
    Registered User
    Join Date
    03-01-2019
    Location
    Nottingham, England
    MS-Off Ver
    2016/365
    Posts
    83

    Re: Export worksheets to new file VBA to 'Break Links' on export?

    It says "Run-time error '1004' We can't make this change for the selected cells because it will affect a pivot table. Use the field list to change the report. If you are trying to insert or delete cells, move the pivot table and try again.

    It's actually half working now, it creates the new file but saves it as 'book1' instead of the name and cell reference as it's suppose to.

    Also I think I see the problem now, the links are broken in the formulas but the pivot is still referencing the original workbook.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Export worksheets to new file VBA to 'Break Links' on export?

    try this...

    Sub ExportNew()
        
        Dim FileName  As String
        Dim Path      As String
        Dim ws        As Worksheet
        Dim pt        As PivotTable
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        Path = "C:\This location\"
        FileName = "New File " & Range("A1").Value & ".xlsx"
        
        Sheets(Array("Data", "Pivot", "Mapping")).Copy
        
        With ActiveWorkbook
            For Each ws In .Worksheets
                For Each pt In ws.PivotTables
                    pt.TableRange2.Copy
                    pt.TableRange2.PasteSpecial Paste:=xlPasteValues
                Next pt
                ws.UsedRange.Value = ws.UsedRange.Value
            Next ws
        
            .SaveAs Path & FileName, xlOpenXMLWorkbook
            .Close False
        End With
        
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        
    End Sub

  7. #7
    Registered User
    Join Date
    03-01-2019
    Location
    Nottingham, England
    MS-Off Ver
    2016/365
    Posts
    83

    Re: Export worksheets to new file VBA to 'Break Links' on export?

    Thanks,

    This is much closer and may suffice as at least there are no links to the source workbook however it would be desirable if it could stay as a pivot as this just shows the pivot as a data table and gets rid of the functionality.

    Maybe it's not possible to do this?

    Thanks for all your help

+ 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] Break links when export to xlxs
    By matt-43 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-28-2020, 12:55 PM
  2. [SOLVED] break links when export sheet sending mail
    By dodde in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-08-2020, 11:27 AM
  3. Export each page break in a pivot table as a separate pdf file
    By tmoreira in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-12-2019, 08:21 PM
  4. Excel to Powerpoint VBA Export - How to auto break links
    By ScottJB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-16-2017, 09:25 AM
  5. Replies: 0
    Last Post: 11-10-2016, 10:18 AM
  6. [SOLVED] How do I break a csv file into smaller size for export?
    By svb in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 12-21-2005, 07:55 PM
  7. [SOLVED] Want Excel to break links between worksheets in same file
    By eclectic_kx in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-25-2005, 02: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