• 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.

How to create an efficient conditional statement to bypass an error?

shahin

Active Member
I've written a macro to get the highest "next page" number from a webpage traversing several pages. My target is to get the highest "next page" number from page 64, 65 and 66. Each individual page has displayed it's items across several pages through pagination. Page 64 has 3 such sub pages, page 66 has also 3 such pages but 65 doesn't have any pagination at all. So , my code breaks when the loop hits the page 65. How can I create any conditional statement to bypass or skip the page with no such pagination?

This is what I've written so far:

Code:
Sub Web_Data()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim elem As Object
   
    For x = 64 To 66
        With http
            .Open "GET", "http://www.pour-les-personnes-agees.gouv.fr/annuaire-accueil-de-jour/" & x & "/0", False
            .send
            html.body.innerHTML = .responseText
        End With

        Set elem = html.getElementsByClassName("next last")(0).getElementsByTagName("a")
        r = r + 1: Cells(r, 1) = Split(elem(0).href, "=")(1)
    Next x
End Sub
 
Hi ,

Try this :
Code:
Sub Web_Data()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim elem As Object
 
    For x = 64 To 66
        With http
            .Open "GET", "http://www.pour-les-personnes-agees.gouv.fr/annuaire-accueil-de-jour/" & x & "/0", False
            .send
            html.body.innerHTML = .responseText
        End With

        Set elem = html.getElementsByClassName("next last")(0)
        If Not elem Is Nothing Then
          Set elem = elem.getElementsByTagName("a")
          r = r + 1: Cells(r, 1) = Split(elem(0).href, "=")(1)
        End If
    Next x
End Sub
Narayan
 
Yes it did the trick. Just one thing to get the clarity: you used the same variable twice other than what I'm doing below (I'm just trying to get a better understanding). There must be any reason.
Code:
Sub Web_Data()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim elem As Object, post As Object
  
    For x = 64 To 66
        With http
            .Open "GET", "http://www.pour-les-personnes-agees.gouv.fr/annuaire-accueil-de-jour/" & x & "/0", False
            .send
            html.body.innerHTML = .responseText
        End With
      
        Set elem = html.getElementsByClassName("next last")(0)
        If Not elem Is Nothing Then
            Set post = elem.getElementsByTagName("a")
            r = r + 1: Cells(r, 1) = Split(post(0).href, "=")(1)
        End If
    Next x
End Sub

Thanks a lot Narayan. It's a long time since I found any solution from you.
 
Hi ,

You could have declared two variables , but since elem is not being used elsewhere with its first assigned value , there is no harm in reusing the same variable.

Narayan
 
I'll practice this lesson in future. Thanks a trillion. You are always a great teacher.
 
Back
Top