VBA - Ignore URL webscrape if link doesn't work

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
Hello I have the following code that pulls the data from the URLs listed in cells A1:A84. How would I modify it please so that it ignores urls that do not have any content?

Many thanks.

Code:
Sub Data()
 Dim URL As Range
 Dim nxRw As Long
 Dim Addrw As Long
 Sheets("Data").Select
 For Each URL In Range("A1:A84")
 nxRw = Range("C" & Rows.Count).End(xlUp).Row + Addrw * 2
 With ActiveSheet.QueryTables.Add(Connection:="URL;" & URL.Value & "", _
     Destination:=Range("C" & nxRw))
     .Name = Right(URL.Value, 27)
     .FieldNames = True
     .RowNumbers = False
     .FillAdjacentFormulas = False
     .PreserveFormatting = True
     .RefreshOnFileOpen = False
     .BackgroundQuery = True
     .RefreshStyle = xlInsertDeleteCells
     .SavePassword = False
     .SaveData = True
     .AdjustColumnWidth = True
     .RefreshPeriod = 0
     .WebSelectionType = xlEntirePage
     .WebFormatting = xlWebFormattingNone
     .WebPreFormattedTextToColumns = True
     .WebConsecutiveDelimitersAsOne = True
     .WebSingleBlockTextImport = False
     .WebDisableDateRecognition = False
     .WebDisableRedirections = False
     .Refresh BackgroundQuery:=False
End With
Addrw = 1
Next URL


End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You have to share some url and explain in detail which url you want to ignore and what you're really want to fetch from remaining urls ?
 
Upvote 0
Hello,

If the URL is false, i.e. a page does not exist, ignore these in the range, which in the above example is A1:A84.

Does that help?
 
Upvote 0
Try this:

Code:
Function Isurlvalid(ByVal url As String) As Boolean
Dim myreq As Object

    Isurlvalid = False
    Set myreq = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    If Not url Like "http*" Then
        url = "http://" & url
    End If
    
    On Error GoTo Notvalid
    
    With myreq
        .Open "GET", url, False
        .send
        If .Status = 200 Then Isurlvalid = True
        Exit Function
    End With
Notvalid:
End Function
 
Upvote 0
Try this:

Code:
Function Isurlvalid(ByVal url As String) As Boolean
Dim myreq As Object

    Isurlvalid = False
    Set myreq = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    If Not url Like "http*" Then
        url = "http://" & url
    End If
    
    On Error GoTo Notvalid
    
    With myreq
        .Open "GET", url, False
        .send
        If .Status = 200 Then Isurlvalid = True
        Exit Function
    End With
Notvalid:
End Function


Sorry, how do I refer to the range where my URLs are located in column A please?

Also, it doesn't appear to run, as no Sub at the start?

Thanks.
 
Upvote 0
Sorry, how do I refer to the range where my URLs are located in column A please?

Also, it doesn't appear to run, as no Sub at the start?

Thanks.


This is a User Defined Function. You can call it passing each url as parameter and check if it returns True or False.

Code:
Sub james()
Dim rng As Range
Dim cell As Range

Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)

For Each cell In rng
    If Isurlvalid(cell.Value) Then
        'Place your other code here
        'Place your other code here
        'Place your other code here
        'Place your other code here
    End If
Next
End Sub
Function Isurlvalid(ByVal url As String) As Boolean
Dim myreq As Object

    Isurlvalid = False
    Set myreq = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    If Not url Like "http*" Then
        url = "http://" & url
    End If
    
    On Error GoTo Notvalid
    
    With myreq
        .Open "GET", url, False
        .send
        If .Status = 200 Then Isurlvalid = True
        Exit Function
    End With
Notvalid:
End Function
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,404
Members
448,893
Latest member
AtariBaby

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