Search for external links using VBA

svjensen

Board Regular
Joined
Jun 10, 2009
Messages
118
I need to search for external links in a workbook using VBA.

I was thinking of doing it along the lines of:

Code:
If Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
    SearchFormat:=False).Activate Then
 
    MsgBox "External Link found!"
 
Else
    MsgBox "No external links found!"
End if

This works if an external link exists, but if no links are found I get the following error message:
Run-time error '91':
Object variable or With block variable not set

How can I achieve what I want?

/Soren
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Ah I see - I wondered if this was a technique I'd never seen before!

You can assign the Find result to a rnage variable (which won't error if nothing is found) and test this instead:

Code:
Dim rngExtLink As Range
 
Set rngExtLink = Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
    SearchFormat:=False)
 
If Not rngExtLink Is Nothing Then
 
    MsgBox "External Link found!"
 
Else
    MsgBox "No external links found!"
End if
 
Upvote 0
Just because there's always more than 1 way..

Code:
If hasLinks Then
 
    MsgBox "External Link found!"
Else
    MsgBox "No external links found!"
End if

Here's the function

Code:
Function hasLinks()
    hasLinks = False
    On Error Resume Next
    hasLinks = (LBound(ActiveWorkbook.LinkSources(xlExcelLinks)) >= 0)
End Function

This might be quicker than performing a find on a particularly data heavy sheet. Of course, it won't tell you where the links are...
 
Upvote 0
Weaver, thanks for following up.

Both concepts work fine for me, as I only need to know if external links exist.

One follow up though. Both concepts will catch external links in formulas (and possible others as well).
Is there a way to catch external links in NamedRanges?

/Soren
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,614
Members
452,661
Latest member
Nonhle

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