Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
ll ll is offline
external usenet poster
 
Posts: 67
Default Removal of External Links -incl script

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Removal of External Links -incl script

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" wrote in message
ups.com...
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
ll ll is offline
external usenet poster
 
Posts: 67
Default Removal of External Links -incl script

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Removal of External Links -incl script

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).

--
Regards,
Tom Ogilvy


"ll" wrote in message
oups.com...
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



  #5   Report Post  
Posted to microsoft.public.excel.programming
ll ll is offline
external usenet poster
 
Posts: 67
Default Removal of External Links -incl script

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



On Feb 14, 4:56 pm, "Tom Ogilvy" wrote:
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 anylinksif thelinksare all references within these three sheets (as an example).

--
Regards,
Tom Ogilvy

"ll" wrote in message

oups.com...

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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro with external script. kurt Excel Programming 1 January 12th 06 10:13 AM
External Reference removal Dave the Wave Excel Programming 4 December 20th 04 07:00 PM
Links, removal & locating david[_6_] Excel Programming 1 September 26th 03 12:29 AM
MS query links removal in VBA Andrea[_5_] Excel Programming 0 September 18th 03 10:21 PM
DDE Links using Microsoft Script Editor Serge[_2_] Excel Programming 0 September 11th 03 10:44 PM


All times are GMT +1. The time now is 12:41 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"