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

Incapable of bringing clarity to function usage

shahin

Active Member
As a vba beginner sometimes i find it hard to understand how function works. If you consider the below example then you can see that the first scraper is running flawlessly. However, when I make my scraper act like same in case it is wrapped within a function, it works no more. It is happening only because i could not propel it the right way. How can i execute my second scraper?

First one (working):
Code:
Sub WebData()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim source As Object

    With http
        .Open "GET", "http://www.brazil4export.com/en/pesquisa/resultado/?page=1&", False
        .send
        html.body.innerHTML = .responseText
    End With
    For Each source In html.getElementsByClassName("panel-heading")
        x = x + 1: Cells(x, 1) = source.getAttribute("data-Name")
        Cells(x, 2) = source.getAttribute("data-site")
    Next source
End Sub

Second one (not working):
Code:
Function GetResult(url As String) As String
    Dim http As New XMLHTTP60, html As New HTMLDocument
    With http
        .Open "GET", url, False
        .send
        html.body.innerHTML = .responseText
    End With
    GetResult = html.body.innerHTML
End Function

Sub Test_result()
    Dim source As Object
    GetResult ("http://www.brazil4export.com/en/pesquisa/resultado/?page=1&")
    For Each source In (what should be here?).getElementsByClassName("panel-heading")
        x = x + 1: Cells(x, 1) = source.getAttribute("data-Name")
        Cells(x, 2) = source.getAttribute("data-site")
    Next source
End Sub
 
Last edited:

You must load result of function in main procedure
- just use a variable ! - as shown in VBA inner help …​
 
Thanks Marc L, for your suggestion. Well, if I use a variable, how can that bridge between function and main procedure? However, I'm having a hard time to understand that as well.
 
Code:
Function Square&(L&)
         Square = L * L
End Function

Sub Main()
    R& = Square(9)
    MsgBox R
End Sub
And with a bit of logic you ever do not neet any variable
as you already saw in my code within your own threads …
 
I got there almost. Now, msgbox in main procedure can display the responsetext but how to execute the uncommented part?

Code:
Function GetResult(url As String) As String
    Dim http As New XMLHTTP60

    With http
        .Open "GET", url, False
        .send
        post = .responseText
    End With
    GetResult = post
End Function

Sub Test_result()
    Dim source As Object, html As New HTMLDocument, post As String

    post = GetResult("http://www.brazil4export.com/en/pesquisa/resultado/?page=1&")
    MsgBox post
'    For Each source In html.getElementsByClassName("panel-heading")
'        x = x + 1: Cells(x, 1) = source.getAttribute("data-Name")
'        Cells(x, 2) = source.getAttribute("data-site")
'    Next source
End Sub
 
Last edited:

First your function must return the correct data type
so comparing initial code it can't be a string !​
 
Hi ,

Try this :
Code:
Function GetResult(url As String) As String
    Dim http As New XMLHTTP60
    Dim post As New HTMLDocument

    With http
        .Open "GET", url, False
        .send
        post.body.innerHTML = .responseText
    End With
    GetResult = post.body.innerHTML
End Function

Sub Test_result()
    Dim source As Object, html As HTMLDocument
    Dim odoc As Object

    Set odoc = CreateObject("HTMLFile") '// late binding

    odoc.body.innerHTML = GetResult("http://www.brazil4export.com/en/pesquisa/resultado/?page=1&")
    Set html = odoc
   
    For Each source In html.getElementsByClassName("panel-heading")
        x = x + 1: Cells(x, 1) = source.getAttribute("data-Name")
        Cells(x, 2) = source.getAttribute("data-site")
    Next source
End Sub
Narayan
 
NARAYAN's solution is not what you initialy asked for :
For Each source In (what should be here?).getElementsByClassName("panel-heading")
So (what should be here) can't be a string but an object !
Just comparing with initial code …
As every beginner coder must know which data type to use !
The very basics of any coding language …
 
@Marc L, I needed to know how to execute a sub procedure if a significant part of which is wrapped within a function. I now at least know how to give it a go. The way I expected in my first post perhaps hard to satisfy or I don't know. It would be great If I could do such way.Thanks.
 
Perhaps this is what I expected. It is working now.
Code:
Function GetResult(url As String) As String
    Dim http As New XMLHTTP60, html As New HTMLDocument
    With http
        .Open "GET", url, False
        .send
        html.body.innerHTML = .responseText
    End With
    GetResult = html.body.innerHTML
End Function

Sub Test_result()
    Dim source As Object, html As New HTMLDocument
    html.body.innerHTML = GetResult("http://www.brazil4export.com/en/pesquisa/resultado/?page=1&")
    For Each source In html.getElementsByClassName("panel-heading")
        x = x + 1: Cells(x, 1) = source.getAttribute("data-Name")
        Cells(x, 2) = source.getAttribute("data-site")
    Next source
End Sub

Thanks Narayan and Marc L.
 
Narayan gave you a fish.
If tomorrow you need another one but if Narayan can't come
to bring you a fish, neither anyone else, you will stay on your own …

My attempt was not to bring you any fish but to show you how to fish,
the Confucius' way !
As here it was just the very lower level of programming, data type …
 
@Marc L, I know your style and I like it very much. You always tried to make me do stuffs on my own by providing with guidelines. That is why I have learnt so quickly. Thanks.
 
Although I do not have a good knowledge on function, I've tried to modify the existing code the way it should be:

Code:
Function GetSource() As String
    Dim http As New ServerXMLHTTP60

    With http
        .Open "GET", "https://yts.ag/browse-movies", False
        .send
        GetSource = .responseText
    End With
End Function

Sub Fetch_Data()
    Dim html As New HTMLDocument
    Dim post As HTMLHtmlElement
   
    html.body.innerHTML = GetSource()
   
    For Each post In html.getElementsByClassName("browse-movie-bottom")
        With post.getElementsByClassName("browse-movie-title")
           If .Length Then Row = Row + 1: Cells(Row, 1) = .Item(0).innerText
        End With
        With post.getElementsByClassName("browse-movie-year")
           If .Length Then Cells(Row, 2) = .Item(0).innerText
        End With
    Next post
End Sub
 
Back
Top