Copy worksheets

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
Hi all

How can i copy worksheets from a workbook then keep the formulas but delete links to previous workbook?

I tried this but no luck.

Please somebody help

VBA Code:
Dim astrLinks As Variant

astrLinks = wkbDest.LinkSources(Type:=xlLinkTypeExcelLinks)


With wkbDest.Sheets(6)
   .Select
   .Unprotect
   .BreakLink Name:=astrLinks(6), _
    Type:=xlLinkTypeExcelLinks

   .Protect
End With
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
LinkSources is a workbook method, not a sheet method.
Try this instead (assumes the only external links are to the previous workbook) - not tested :
VBA Code:
Dim cel As Range
For Each cel In wkbDest.Sheets(6).Cells.SpecialCells(xlCellTypeFormulas)
     If InStr(cel.Formula, "[") Then cel = cel.Value
Next
 
Upvote 0
The code takes away all formulas but they need to stay but links to previous worksheet broken
 
Upvote 0
The code takes away all formulas but they need to stay but links to previous worksheet broken
It should not remove all formulas - only those with external links. Is that not what you want?
 
Upvote 0
Sorry I meant below,

The code takes away all formulas but they need to stay but links to previous workbook broken.
The formulas take their data from the same name worksheet on both workbooks.
If that makes sense
 
Upvote 0
Try this :

VBA Code:
wkbDest.Sheets(6).Cells.SpecialCells (xlCellTypeFormulas)
    .Replace What:="[*]", Replacement:="", LookAt:=xlPart
 
Upvote 0
Hi and Good Morning

I`ve tried this but the (. Replace) goes blue?? And says the Invalid or unqualified reference error.

Mt
 
Upvote 0
VBA Code:
wkbDest.Sheets(6).Cells.SpecialCells (xlCellTypeFormulas) _
    .Replace What:="[*]", Replacement:="", LookAt:=xlPart
 
Upvote 0
This is my code below for taking all worksheets to a new workbook. I`ve added your code to this but it still links to the previous workbook.
All I need is the formulas on Sheet 6 to transfer to a new workbook along with the rest of the sheets. The rest of the sheets are a straight copy & paste

VBA Code:
Private Sub ListBox3_Click()

 
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    
    Set wkbDest = Workbooks("Automated Cardworker.xlsm")

          Do While wkbDest.Sheets.Count > 4
            wkbDest.Sheets(wkbDest.Sheets.Count).Delete
            Loop
        

    Set wkbSource = Workbooks.Open("\\TGS-SRV01\Share\ShopFloor\PRODUCTION\DLS Cardworker\Jobcard Templates\" _
                         & Body_And_Vehicle_Type_Form.ListBox3.List(Body_And_Vehicle_Type_Form.ListBox3.ListIndex))

        For Each sht In Workbooks(wkbSource.Name).Sheets
           sht.Copy After:=wkbDest.Sheets(wkbDest.Sheets.Count)
        Next sht
        
wkbSource.Close SaveChanges:=False

wkbDest.Sheets(6).Cells.SpecialCells(xlCellTypeFormulas) _
    .Replace What:="[*]", Replacement:="", LookAt:=xlPart

wkbSource.Close SaveChanges:=False

Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
 
Upvote 0
Are you sure it's Sheets(6) ? Maybe it's Sheets("Sheet6"), or maybe Sheet6 ? Check the VB Editor for the sheet ref.
What sheet gets activated with :
VBA Code:
wkbDest.Activate
Sheets(6).Activate
 
Upvote 0

Forum statistics

Threads
1,215,209
Messages
6,123,646
Members
449,111
Latest member
ghennedy

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