Hi Guys,
I am using below code to validate URL's on a excel and highlight the cell color respective of the validation. In this code for both 404 error page and redirection the cell is getting highlighted in dark grey. But, I need to change different color for redirection alone.
Since, I am very new to VBA couldn't able to figure it out myself. I have googled and combined this code so far.
Can somebody help please?
Thanks in Advance.
Sub Validate_URLs_on_this_sheet() Dim tgtSheet As Worksheet
Set tgtSheet = ActiveWorkbook.ActiveSheet
ValidateURLs tgtSheet
End Sub
Private Sub ValidateURLs(tgtSheet)
Dim tgtLastRow, tgtRowCount, validURL As Integer
Dim workURL As String
'Get Last Row of entries - Last Row of data in target sheet
tgtLastRow = tgtSheet.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'init tgtRowCount as second row (header is first row)
tgtRowCount = 2
'loop through each row in sheet
Do While tgtRowCount <= tgtLastRow
'if not validated yet (not colored), validate this row's URL
If tgtSheet.Cells(tgtRowCount, 2).Interior.ColorIndex < 0 Then
workURL = tgtSheet.Cells(tgtRowCount, 2).Value
'check if it's valid
validURL = URLExists(workURL)
'if URL is valid
If validURL = 4 Then
'color the cell light green
tgtSheet.Cells(tgtRowCount, 2).Interior.Color = 11073212 '14348258
'elseif length is too short
ElseIf validURL = 1 Then
'color the cell light grey
tgtSheet.Cells(tgtRowCount, 2).Interior.Color = 14277081
'elseif no length returned, and not chunked - manual check - something wrong?
ElseIf validURL = 0 Then
'color the cell light red
tgtSheet.Cells(tgtRowCount, 2).Interior.Color = 4934655
'else not valid path
Else
'color the cell grey
tgtSheet.Cells(tgtRowCount, 2).Interior.Color = 8421504
End If
End If
tgtRowCount = tgtRowCount + 1
Loop
End Sub
Function URLExists(url As String) As Integer
'Returns: -1 if invalid URL; 0 if unsure URL; 1 if short URL; 4 if valid URL
Dim Request As Object
Dim cl As Long
Dim te As String
cl = -1
te = ""
On Error Resume Next
Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
With Request
.SetTimeouts 0, 60000, 60000, 60000
'Set AT&T Proxy
.SetProxy 2, "one.proxy.att.com:8080"
'.SetRequestHeader "Accept-Encoding", "gzip" 'Force gzip to avoid no-length returns
'.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"
'GET page (can't use HEAD since we want length returned
.Open "GET", url, False
'Disable redirects so we capture if one is happening
.Option(6) = False
.Send
'if a valid page status is returned
'MsgBox (.Status)
'MsgBox (.Option(WinHttpRequestOption_URL))
'MsgBox (.GetAllResponseHeaders())
If .Status = 200 Then
'get response length - if not present, cl will stay -1
cl = .GetResponseHeader("Content-Length")
'get transfer encoding used
te = LCase(.GetResponseHeader("Transfer-Encoding"))
'if no length returned
If cl = -1 Then
'if transfer is chunked
If te = "chunked" Then
'URL is valid
URLExists = 4: Exit Function
'else should be a single block with length but isn't
Else
'URL is funky - check manual
URLExists = 0: Exit Function
End If
'if page is zero length
ElseIf cl = 0 Then
'URL invalid as nothing actually there
URLExists = -1: Exit Function
'if the page is too short to be valid
ElseIf cl < 40 Then
'manual check
URLExists = 1: Exit Function
'else the URL is valid and long enough to be "real"
Else
'URL is valid
URLExists = 4: Exit Function
End If
'else a valid page status was not returned (301, 302, 404, etc.)
Else
'URL is invalid
URLExists = -1: Exit Function
End If
End With
'void the object
Set Request = Nothing
MsgBox ("shouldn't be here")
ErrorHandle:
'should never reach here, but failsafe "manual" value
URLExists = 0
'-2147012746 - header not found
End Function
Display More