• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Broken Links in Excel 2007

ajayxlnc

New Member
Hello,


I have an excel file with 4297 Hyperlinks to the external sites. I need a VBA code which can run the code and check for the broken links. I want to know how many links are active and how many are broken. Please help me out in this issue


Here is the Link


https://sites.google.com/site/ajayxlnc/software/OnlyLinks.xlsx
 
Ajayxlnc


Firstly, Welcome to the Chandoo.org Forums


A small point, your list is a list of URL's, not a list of Hyperlinks


I'm not an expert in web access and so will provide some code and let you play with it.


Please read instructions below the code


- - - - - -

[pre]
Code:
Declare PtrSafe Function InternetGetConnectedState Lib "wininet" (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long
'Delete the PtrSafe word above if not on a 64 bit machine

Sub Clear_URLs()
Dim oLink As Hyperlink
Dim sFile As Boolean
Dim ws As Worksheet
Dim rCell As Range

For Each ws In ActiveWorkbook.Worksheets
For Each rCell In ActiveSheet.UsedRange.Cells
sFile = GetHTML("Http://" + rCell.Text)
If sFile Then
rCell.Interior.Color = 65535 'Highlight Good Links Yellow
Else
rCell.Interior.Color = 255 'Highlight Bad Links Red
'rCell.ClearContents
End If
Next
Next
End Sub

Function GetHTML(URL As String) As Boolean
Dim oHTTP As MSXML2.XMLHTTP
GetHTML = False 'Set default

If Len(URL) Then
Set oHTTP = New MSXML2.XMLHTTP
oHTTP.Open "GET", URL, False
On Error Resume Next
oHTTP.send
If Len(oHTTP.responseText) Then GetHTML = True 'This does the deed
Set oHTTP = Nothing
End If
End Function
[/pre]
- - - - - -


In addition to this you need to go into VBA, Tools, References

Scroll down to the Microsoft XML, v6.0

or Tick the highest number you have if you don't have 6.0


Also note the comment

'Delete the PtrSafe word above if not on a 64 bit machine


Run the subroutine Clear_URLs


The problem with HP URL's is that although the URL maynot exist HP redirects you somewhere else.


So this code works well for sites that don't do that

But doesn't work well for sites that do, like yours.


Hopefully somebody who understands what Microsoft XML or MSXML2.XMLHTTP objects can chip in here and assist?


Please let us know how you go?
 
If anybody is interested

This code will do the same for actual hyperlinks in cells


- - - - - - - -

[pre]
Code:
Declare PtrSafe Function InternetGetConnectedState Lib "wininet" (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long

Sub Clear_Hyperlinks()
Dim oLink As Hyperlink
Dim sFile As Boolean
Dim ws As Worksheet
Dim rCell As Range

For Each ws In ActiveWorkbook.Worksheets
For Each rCell In ActiveSheet.UsedRange.Cells
If rCell.Hyperlinks.Count > 0 Then
For Each oLink In rCell.Hyperlinks
sFile = GetHTML(oLink.Address)
If sFile Then
rCell.Interior.Color = 65535 'Highlight Good Links Yellow
Else
rCell.Interior.Color = 255 'Highlight Good Links Red
'rCell.ClearContents
End If
Next
End If
Next
Next
End Sub

Function GetHTML(URL As String) As Boolean
Dim oHTTP As MSXML2.XMLHTTP
GetHTML = False 'Set default

If Len(URL) Then
Set oHTTP = New MSXML2.XMLHTTP
oHTTP.Open "GET", URL, False
On Error Resume Next
oHTTP.send
If Len(oHTTP.responseText) Then GetHTML = True 'This does the deed
Set oHTTP = Nothing
End If
End Function
[/pre]

- - - - - - - -


In addition to this you need to go into VBA, Tools, References

Scroll down to the Microsoft XML, v6.0

or Tick the highest number you have if you don't have 6.0


Also note the comment

'Delete the PtrSafe word above if not on a 64 bit machine


Run the subroutine Clear_Hyperlinks


The code as is highlights Good Hyperlinks Yellow and Bad Hyperlinks Red


Change the 2 lines

rCell.Interior.Color = 255 'Highlight Good Links Red

'rCell.ClearContents


to


'rCell.Interior.Color = 255 'Highlight Good Links Red

rCell.ClearContents


to clear the Hyperlinks
 
Back
Top