Removal of External Links -incl script

L

ll

I've been working with a vba script that works just fine when run from
the workbook from which it will remove links to external workbooks;
however, when I set the script up in one workbook to create a new
workbook, and then I activate and call the script from there, it fails
to remove the links. I've put msg boxes in place to check to be sure
that the script does run, and it does, although the links fail to
disappear.

Thanks for any help you might be able to provide.-Louis
Here is what I have:

=======
'This is the main module that calls the link remover ("Should
Delete"):

Sub timesheetGenerate()
Dim fillAmt As Long

'Open existing timesheet

Workbooks.Open Filename:=UserForm1.TextBox1.Value

LOldWb = ActiveWorkbook.Name

'////Add new workbook
Workbooks.Add
LNewWb = ActiveWorkbook.Name

'////Copy sheet one of existing timesheet
Windows(LOldWb).Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

'////Paste values from existing timesheet/sheet one, to the new
timesheet/sheet one.
Windows(LNewWb).Activate
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats,
Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False

'////Populate sheet two of new timesheet with date range, in column A
Windows(LNewWb).Activate
Sheets("Sheet2").Select
Cells(1, 1).Select
'////populate cell A1 with the beginning date, from the vba form box
ActiveCell.Value = UserForm1.TextBox4.Value

'////fill the rest of the dates, using the fillAmt variable
fillAmt = UserForm1.TextBox3.Value
Selection.AutoFill Destination:=Range("A1:A" & fillAmt),
Type:=xlFillSeries

'////Copy sheet three of existing timesheet
Windows(LOldWb).Activate
Sheets("Sheet3").Select
Cells.Select
Selection.Copy

'////Paste values from existing timesheet/sheet three, to the new
timesheet/sheet three.
Windows(LNewWb).Activate
Sheets("Sheet3").Select
Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats,
Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False

'////Remove external links :)
Should_Delete

'////Save new timesheet and close
ActiveWorkbook.SaveAs Filename:=UserForm1.TextBox2.Value

ActiveWorkbook.Close

'////Close old timesheet
Windows(LOldWb).Activate
ActiveWorkbook.Close

End Sub

============================================

This is the "Should Delete" script:

'Option Base 1

'This macro deletes all formula links in a workbook.
'
'This macro does not delete a worksheet formula that references an
open
'book, for example:
'
' =[Book1.xls]Sheet1!$A$1
'
' To delete only the links in the active sheet, see the comments
' provided in the Delete_It macro later in this article.

Public Times As Integer
Public Link_Array As Variant

Sub Should_Delete()
Items = 0 'initialize these names
Times = 0
Link_Array = ActiveWorkbook.LinkSources 'find all document links

Items = UBound(Link_Array) 'count the number of links
For Times = 1 To Items

'Ask whether to delete each link
Msg = "Do you want to delete this link: " & Link_Array(Times)
Style = vbYesNoCancel + vbQuestion + vbDefaultButton2
response = MsgBox(Msg, Style)
If response = vbYes Then Delete_It
Delete_It
If response = vbCancel Then Times = Items
Next Times
End Sub

Sub Delete_It()
Count = Len(Link_Array(Times))
For Find_Bracket = 1 To Count - 1
'Replace the "\" in the next line with a ":" if you are using
'Microsoft Excel for the Macintosh.
If Mid(Link_Array(Times), Count - Find_Bracket, 1) = "\" _
Then Exit For

Next Find_Bracket
'Add brackets around the file name.
With_Brackets = Left(Link_Array(Times), Count - Find_Bracket) & _
"[" & Right(Link_Array(Times), Find_Bracket) & "]"

'Does the replace.

'If you want to remove links only on the active sheet, change the
'next two lines into comments by placing an (') apostrophe in
front of
'them as well as the line, "Next Sheet_Select", that closes the
loop.

For Each Sheet_Select In ActiveWorkbook.Worksheets

Sheet_Select.Activate
Set Found_Link = Cells.Find(what:=With_Brackets,
After:=ActiveCell, LookIn:=xlFormulas, lookat:=xlPart,
searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
While UCase(TypeName(Found_Link)) <> UCase("Nothing")

Found_Link.Activate

On Error GoTo anarray

Found_Link.Formula = Found_Link.Value

Msg = "Do you want to delete formula also?"

Style = vbYesNo + vbQuestion + vbDefaultButton2

response = MsgBox(Msg, Style)

If response = vbYes Then
Found_Link.Formula = Found_Link.Value

Else

new_formula = Application.Substitute(Found_Link.Formula,
With_Brackets, "")

Found_Link.Formula = new_formula

End If

Set Found_Link = Cells.FindNext(After:=ActiveCell)

Wend
Next Sheet_Select 'To remove links only on the active sheet
'place an (') apostrophe at the front of this
line.

MsgBox "loop"

Exit Sub

anarray:
Selection.CurrentArray.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues
Resume Next

End Sub
 
T

Tom Ogilvy

I scanned through your code, and I got the impression that you want to
maintain your linking formulas, but have them link to the current workbook.
One way would be to avoid the links. If you copy all the involved sheets at
once, then you won't have these external links.

bk1.Worksheets(Array("Sheet1","Sheet5","Data1")) _
.copy After:=bk.worksheets(bk.worksheets.count)

then you can work on each copied sheet if you need to make changes

--
Regards,
Tom Ogilvy



ll said:
I've been working with a vba script that works just fine when run from
the workbook from which it will remove links to external workbooks;
however, when I set the script up in one workbook to create a new
workbook, and then I activate and call the script from there, it fails
to remove the links. I've put msg boxes in place to check to be sure
that the script does run, and it does, although the links fail to
disappear.

Thanks for any help you might be able to provide.-Louis
Here is what I have:

=======
'This is the main module that calls the link remover ("Should
Delete"):

Sub timesheetGenerate()
Dim fillAmt As Long

'Open existing timesheet

Workbooks.Open Filename:=UserForm1.TextBox1.Value

LOldWb = ActiveWorkbook.Name

'////Add new workbook
Workbooks.Add
LNewWb = ActiveWorkbook.Name

'////Copy sheet one of existing timesheet
Windows(LOldWb).Activate
Sheets("Sheet1").Select
Cells.Select
Selection.Copy

'////Paste values from existing timesheet/sheet one, to the new
timesheet/sheet one.
Windows(LNewWb).Activate
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats,
Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False

'////Populate sheet two of new timesheet with date range, in column A
Windows(LNewWb).Activate
Sheets("Sheet2").Select
Cells(1, 1).Select
'////populate cell A1 with the beginning date, from the vba form box
ActiveCell.Value = UserForm1.TextBox4.Value

'////fill the rest of the dates, using the fillAmt variable
fillAmt = UserForm1.TextBox3.Value
Selection.AutoFill Destination:=Range("A1:A" & fillAmt),
Type:=xlFillSeries

'////Copy sheet three of existing timesheet
Windows(LOldWb).Activate
Sheets("Sheet3").Select
Cells.Select
Selection.Copy

'////Paste values from existing timesheet/sheet three, to the new
timesheet/sheet three.
Windows(LNewWb).Activate
Sheets("Sheet3").Select
Selection.PasteSpecial Paste:=xlPasteFormulasAndNumberFormats,
Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False

'////Remove external links :)
Should_Delete

'////Save new timesheet and close
ActiveWorkbook.SaveAs Filename:=UserForm1.TextBox2.Value

ActiveWorkbook.Close

'////Close old timesheet
Windows(LOldWb).Activate
ActiveWorkbook.Close

End Sub

============================================

This is the "Should Delete" script:

'Option Base 1

'This macro deletes all formula links in a workbook.
'
'This macro does not delete a worksheet formula that references an
open
'book, for example:
'
' =[Book1.xls]Sheet1!$A$1
'
' To delete only the links in the active sheet, see the comments
' provided in the Delete_It macro later in this article.

Public Times As Integer
Public Link_Array As Variant

Sub Should_Delete()
Items = 0 'initialize these names
Times = 0
Link_Array = ActiveWorkbook.LinkSources 'find all document links

Items = UBound(Link_Array) 'count the number of links
For Times = 1 To Items

'Ask whether to delete each link
Msg = "Do you want to delete this link: " & Link_Array(Times)
Style = vbYesNoCancel + vbQuestion + vbDefaultButton2
response = MsgBox(Msg, Style)
If response = vbYes Then Delete_It
Delete_It
If response = vbCancel Then Times = Items
Next Times
End Sub

Sub Delete_It()
Count = Len(Link_Array(Times))
For Find_Bracket = 1 To Count - 1
'Replace the "\" in the next line with a ":" if you are using
'Microsoft Excel for the Macintosh.
If Mid(Link_Array(Times), Count - Find_Bracket, 1) = "\" _
Then Exit For

Next Find_Bracket
'Add brackets around the file name.
With_Brackets = Left(Link_Array(Times), Count - Find_Bracket) & _
"[" & Right(Link_Array(Times), Find_Bracket) & "]"

'Does the replace.

'If you want to remove links only on the active sheet, change the
'next two lines into comments by placing an (') apostrophe in
front of
'them as well as the line, "Next Sheet_Select", that closes the
loop.

For Each Sheet_Select In ActiveWorkbook.Worksheets

Sheet_Select.Activate
Set Found_Link = Cells.Find(what:=With_Brackets,
After:=ActiveCell, LookIn:=xlFormulas, lookat:=xlPart,
searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
While UCase(TypeName(Found_Link)) <> UCase("Nothing")

Found_Link.Activate

On Error GoTo anarray

Found_Link.Formula = Found_Link.Value

Msg = "Do you want to delete formula also?"

Style = vbYesNo + vbQuestion + vbDefaultButton2

response = MsgBox(Msg, Style)

If response = vbYes Then
Found_Link.Formula = Found_Link.Value

Else

new_formula = Application.Substitute(Found_Link.Formula,
With_Brackets, "")

Found_Link.Formula = new_formula

End If

Set Found_Link = Cells.FindNext(After:=ActiveCell)

Wend
Next Sheet_Select 'To remove links only on the active sheet
'place an (') apostrophe at the front of this
line.

MsgBox "loop"

Exit Sub

anarray:
Selection.CurrentArray.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues
Resume Next

End Sub
 
L

ll

Thanks Tom,
How would the code you provided be implemented? Is there a bit more
code that goes with that?

Also, as there are 'local' (to the local workbook) formulas that I
want to keep, I do need to copy the formulas, rather than the values.

Thanks again,
Louis
 
T

Tom Ogilvy

If you were copying Sheet1, Sheet5 and Data1 separately with separate copy
command in your code, you would do it all at once, then process each sheet
with your code except that you shouldn't need to change any links if the
links are all references within these three sheets (as an example).
 
L

ll

Tom,
Thanks for your help recently with my Excel (removing external links)
question. I am wanting to learn how to do it as you suggested (being
new to VBA). I was wondering if your code provided would need any
modification for a standard 3 sheet workbook or if the sheets in your
array might need to be renamed?

Thanks again,
Louis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top