+ Reply to Thread
Results 1 to 19 of 19

Google search using VBA

  1. #1
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Google search using VBA

    Hello all,

    hope you guys doing great and keep healthy.

    I found a code to do google search from Excel.
    Basically put keyword on A column, then it will shown first result in B column.
    It working fine, how ever, i curious to know how to modify the result, not show the text, but the link.

    I have no idea how to change getelementsbytagname("cite")(0) so the result will be a link.
    Appreciate if anyone can help.

    here's the full code:
    Please Login or Register  to view this content.
    Thank you very much.
    Attached Files Attached Files

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    are you using any libraries in VBA? there are many that are relevant:

    - HTMLobjectLibrary
    - firefox library
    etc, etc...

    furthermore, this was written a long time ago that covers almost anything basic when it comes to automating IE. but now that MS has forced people to switch to edge, some of what is written here has to be changed:

    https://www.access-programmers.co.uk...ng-vba.176968/

    you said something about a link. links in HTML are attached to <a> tags. the relevant attribute is: href. that is the standard in HTML 4.0 and 5.0. you can pull it by pointing to the < a > tag collection.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Google search using VBA

    You could start by changing 'cite' to 'a'.
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Google search using VBA

    Hi,

    Thank you for taking time reply my thread.
    I didn't check about HTML object and firefox library, just copy paste the code and it working good.

    I tried to change the attribute to "a", seem working great. the result is as i expected at the moment. I can pull link of google first result into Excel.

    Just thinking for next move.
    Open the link that i just got, then pull data by the right attribute.

    from here

    ---------------------
    For i = 2 To lastRow

    url = "https://www.google.com/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)

    ------------------

    to here
    For i = 2 To lastRow

    url = Cells(i, 1)

    ------------------

    Do i correct?

  5. #5
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    well the first thing you need to correct is your query string syntax. if you have more than one query arg, then you need ""+"" signs between them. here is goog's protocol for searching strings:
    Please Login or Register  to view this content.
    etc, etc, etc....

    other than that, are you asking anything else? can't tell.

  6. #6
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Google search using VBA

    Quote Originally Posted by Norie View Post
    You could start by changing 'cite' to 'a'.
    Thank you. yea, "a" return with link as needed.

    However i have run the data to 700 row, then it stuck. return with error 91.
    does the problem from the firefox or google? i didn't change any code, so i am sure the code work at first.

    Quote Originally Posted by vba_php View Post
    well the first thing you need to correct is your query string syntax. if you have more than one query arg, then you need ""+"" signs between them. here is goog's protocol for searching strings:
    Please Login or Register  to view this content.
    etc, etc, etc....

    other than that, are you asking anything else? can't tell.
    What i mean is, if i want only to use link from the excel cell and remove "https://www.google.com/search?"
    example in cell A2 : www.example.com

    i modify the code to : url = Cells(i, 1) but doesn't work.

  7. #7
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    upload ur file

  8. #8
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Google search using VBA

    By the way,

    for previous file, to get google result is solved, it's just my internet connection is lost,causing error 91.


    Here's second code. to pull single data from url.
    Here's my current file, still get error : XMLHTTP.Open "GET", url, False

    Thank you.
    Attached Files Attached Files

  9. #9
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    I have ver 2016 excel. what do you have? I have 2 XML libs by default. v.3 and v.6. putting a ref to 6.0 causing a conflict and I don't have time to
    debug it. Furthermore, apparently v.3, which is what i set to test your code, does not have the HTTP request object you are using. see image 1 below.
    can you post a screenshot of what refs you have set? thanks.
    Attached Images Attached Images

  10. #10
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Google search using VBA

    Hi,

    Thank you. I just realize that i just remove double quote on url =Cells(i,1) and XMLHTTP.Open "GET", url, False problem is gone.

    I just don't know how determine element ID and tag to get "SIC Code 8049" on given url example.

  11. #11
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    LOL. simple. always happens.
    Quote Originally Posted by qiyusi View Post
    I just don't know how determine element ID and tag to get "SIC Code 8049" on given url example.
    to get an element's value by capturing it's ID attribute, use javascript's native function:
    Please Login or Register  to view this content.
    does that work with the XML lib you are using? my guess is that it does, or something similar.

  12. #12
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Google search using VBA



    Yes it work.
    Just confuse which element ID or tag to use. to get 8049 on attached screenshot.
    Attached Images Attached Images

  13. #13
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    that's a SPAN silly! it's NOT an anchor tag! does it actually appear as a hyperlink on the screen? because there are NO A TAGS anywhere your screen shot. the span is inside an ordered list, encapsulated by an unordered list. that is a standard protocol for navigation bars and primary menus of business websites. the article I showed you earlier does not cover spans. try this first, if you have not:

    https://www.google.com/search?q=vba+get+span+innerhtml'

    there are many returns there. more than likely you can get it done. let us know if you can't get it. if you can't, post the CSS file's code that houses the class called text-sm md:text-base.

    or, that may even represent a nest in CSS code. I don't use "":"" symbols, colon symbols, so I'm not sure.

  14. #14
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Google search using VBA

    Tried to search information as suggested.
    Still no luck

  15. #15
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    I don't think you can actually capture class content so as far as I know the only chance you've got to get it is to actually click on the link with your mouse and then get the URL from the resulting page but if it's got a token in it that's generated dynamically there's no way you can get it because it changes every time you click the link

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Google search using VBA

    vba_php

    You don't need to set any references for the OP's original post to work.

  17. #17
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    Quote Originally Posted by Norie View Post
    vba_php

    You don't need to set any references for the OP's original post to work.
    can you offer anything else? I set refs all the time in a variety of languages. why not help him/her out?

  18. #18
    Forum Contributor
    Join Date
    10-22-2014
    Location
    Indonesia
    MS-Off Ver
    2010
    Posts
    177

    Re: Google search using VBA

    Hello,

    Back to original thread.
    I consider to close this thread, as the goal to have Excel VBA to get first result of Google is reached.

    Thank you for vba_php for helping me

  19. #19
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Google search using VBA

    Well if it was me that truly helps you out sure you're welcome :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] search data and display results like a Google type search box
    By cfcoetzer in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-16-2020, 07:54 AM
  2. VBA Excel Google Search Address and Pull First Search Result
    By senker in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2017, 11:21 PM
  3. VBA to automate site-search or Google search
    By ks_1029 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-15-2017, 01:37 AM
  4. [SOLVED] clicking second search results from Google search
    By Megatronixs in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-10-2015, 12:36 AM
  5. Google Search export to Excel
    By sergiol in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-30-2013, 02:32 PM
  6. Using VBA to run a Google Search
    By jpcsolutions in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-15-2011, 12:43 PM
  7. Replies: 0
    Last Post: 11-05-2009, 04:47 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1