tesla764
asked on
Excel VBA code breaking of links
Is there a way in VBA to break the link in a spread sheet instead of breaking the link manually. See the provided screen shot for clarification.
Thanks in advance.
EE-VBA---break-the-link-in-a-spr.docx
Thanks in advance.
EE-VBA---break-the-link-in-a-spr.docx
You should be able to loop through the links in the workbook and break them that way:
Matt
Sub sBreakLinks()
Dim vLinks As Variant, i As Long
vLinks = ActiveWorkbook.LinkSources(xlLinkTypeExcelLinks)
For i = 1 To UBound(vLinks)
ActiveWorkbook.BreakLink Name:=vLinks(i), Type:=xlLinkTypeExcelLinks
Next
End Sub
Matt
ASKER
Hi Matt,
When the statement is executed...
For i = 1 UBound(vLinks)
The following error occurs...
Run-time error '13':
Type mismatch
When the statement is executed...
For i = 1 UBound(vLinks)
The following error occurs...
Run-time error '13':
Type mismatch
ASKER
may be I need to place the code ina more strategic place. Will this error occur if the workbook is not active?
It is coded to work on the active workbook, but could be changed if you wanted to run it in the background or something.
I didn't write anything to check if there were no links; here is an updated version:
I didn't write anything to check if there were no links; here is an updated version:
Sub sBreakLinks()
Dim vLinks As Variant, i As Long
vLinks = ActiveWorkbook.LinkSources(xlLinkTypeExcelLinks)
If IsEmpty(vLinks) Then
MsgBox "No links to remove."
Else
For i = 1 To UBound(vLinks)
ActiveWorkbook.BreakLink Name:=vLinks(i), Type:=xlLinkTypeExcelLinks
Next
End If
End Sub
ASKER
When the code hits the line...
vLinks = ActiveWorkbook.LinkSources (xlLinkTyp eExcelLink s)
vLinks is empty
and then that triggers the line...
MsgBox "Nolinks to remove."
When actually the workbook "DRA Summary.xlsx" does have links.
Just as a test I commented out the line...
'vLiknks = ActiveWorkbook.LinkSources (xlLinkTyp eExcelLink s)
and put in the line...
vLinks = "DRA Summary.xlsx"
I still get...
The following error occurs...
Run-time error '13':
Type mismatch
As another test I took your code out of the main Workbokk "DRA Tool.xlsm" ran the program through which creates the "DRA Summary.xlsx" workbook and then applied your code in "DRA Summary.xlsx" and that worked successfully.
I am hoping there is a way to break the link in the "DRA Summary.xlsx" that is being created while running the program "DRA Tool.xlsm", so that when "DRA Summary.xlsx" is created the links will not be there.
vLinks = ActiveWorkbook.LinkSources
vLinks is empty
and then that triggers the line...
MsgBox "Nolinks to remove."
When actually the workbook "DRA Summary.xlsx" does have links.
Just as a test I commented out the line...
'vLiknks = ActiveWorkbook.LinkSources
and put in the line...
vLinks = "DRA Summary.xlsx"
I still get...
The following error occurs...
Run-time error '13':
Type mismatch
As another test I took your code out of the main Workbokk "DRA Tool.xlsm" ran the program through which creates the "DRA Summary.xlsx" workbook and then applied your code in "DRA Summary.xlsx" and that worked successfully.
I am hoping there is a way to break the link in the "DRA Summary.xlsx" that is being created while running the program "DRA Tool.xlsm", so that when "DRA Summary.xlsx" is created the links will not be there.
ASKER
Is it true that the work book MUST be in an active state in order to break the links?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When the code tries to execute the line...
Set vWB = Workbooks("DRA Summary.xlsx")
This error occurs...
Run-time error '9':
Subscript out of range.
Set vWB = Workbooks("DRA Summary.xlsx")
This error occurs...
Run-time error '9':
Subscript out of range.
ASKER
When I check vWB the value is Nothing.
When I check vLinks the value is Empty.
When I check vLinks the value is Empty.
ASKER
If the workbook DOES have to be open to break the links, then that changes the logic. After "DRA Summary.xlsx" is created I guess I would have to open it back up to break the links with the code.
Does that sound right?
Does that sound right?
ASKER
Perhaps this would be a solution...
Open the workbook
Apply the break link code
and then save the workbook without the links.
Open the workbook
Apply the break link code
and then save the workbook without the links.
My apologies, I thought you were asking if the workbook has to be active, not open. Yes, absolutely the workbook has to be open for this to work. The code can open the file if you need it to though
Set vWB = Workbooks.Open("C:\DRA Summary.xlsx")
Then at the bottom to close it again you can paste in
vWB.Close True
Set vWB = Workbooks.Open("C:\DRA Summary.xlsx")
Then at the bottom to close it again you can paste in
vWB.Close True
ASKER
I believe I will have to experiment with this more and perhaps look at a new stategy to make this work properly.
I really appreciate all your help Matt. I have to leave here in 45 minutes and I need to rap up and I will pick this back up on Monday.
Thanks again and have a great weekend.
I really appreciate all your help Matt. I have to leave here in 45 minutes and I need to rap up and I will pick this back up on Monday.
Thanks again and have a great weekend.
Range("D4").Select
Selection.Hyperlinks.Delet
Removes link in D4