+ Reply to Thread
Results 1 to 4 of 4

Automate click "Select" button using vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-21-2013
    Location
    KL
    MS-Off Ver
    Excel 2010
    Posts
    168

    Automate click "Select" button using vba

    Hi there,

    I am trying to duplicate the caode I found from the following website:
    http://www.exceltrainingvideos.com/h...ing-excel-vba/

    However, my code didnt work as expected, i.e. it does not automate the clicking on the "Select" button.
    the source code is this....... input type="submit" value="Select"

    I am just duplicating the code... however it doesn't work.
    It did give any error message. The macro just skip the code when I debugged it using F8.

    For Each html_element In html.getElementsByTagName("input")
        If html_element.Value = "submit" Then html_element.Click: Exit For
    Next
    Can someone point out my mistake?

    Thank you.

    HC

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,747

    Re: Automate click "Select" button using vba

    That post you copied your code from is at least 2 years old. The problem with writing code to interact with web sites is that the site can change.

    It looks like Google has updated their approach to account management since that code was written. If you log into Gmail for the first time with no history, the screen first asks for an email address and has a Next button to move forward. There is no box to enter a password. Your code errors at that point but the code (unwisely) ignores the error and continues. Then you have to press a Next button to move to the next screen to enter your password.

    Once you have logged in using this email address, the next time to login you will not see the first screen, and will be taken directly to the second screen.

    This should all be evident to you as you F8 through your code while watching the Internet Explorer window at the same time.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  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: Automate click "Select" button using vba

    Why not just submit the form?

    Without seeing the rest of your code I can only guess but something like this might work.
    html.forms(0).submit
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    11-21-2013
    Location
    KL
    MS-Off Ver
    Excel 2010
    Posts
    168

    Re: Automate click "Select" button using vba

    Update: The code is working fine now after adding this:

    Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
    Thank you.

    Quote Originally Posted by Norie View Post
    Why not just submit the form?

    Without seeing the rest of your code I can only guess but something like this might work.
    html.forms(0).submit
    Hi Norie,

    Thank you for the reply and apologize for the late reply. Was away to overseas.
    I do not know how to submit the forms... would you be able to help me?
    Below is my code:

    Sub GetOptionData()
        Dim ie      As internetexplorer
        Dim html    As HTMLDocument
        Dim myURL   As String
        
        On Error GoTo Err_Clear
        
        myURL = Range("myURL")
        
        '/ 1st page
        '/ Open Internet Explorer in memory go to website
        '/ Wait until IE has loaded the web page
        Set ie = New InternetExplorerMedium
        With ie
            .Visible = True
            .navigate "myURL"
            Do Until .readyState = 4: DoEvents: Loop
        End With
        
        '/  Web page loaded completely, define variables
        Set html = ie.document
        
        '/ Automate drop down list
        '/ Select region from drop down
        Dim drp As HTMLFormElement
        Set drp = html.getElementById("regionid")
        drp.selectedIndex = 1 '/ select region - 0 Europe, 1 JAPA
    
        Do Until ie.readyState = 4: DoEvents: Loop
        
        '/ Declares variables after region is selected and webpage loaded completely
        Dim html_element    As IHTMLElement
        Dim html_filter     As HTMLDocument
        
        '/ Look for Select button and automate click
        Set html_filter = ie.document
        For Each html_element In html_filter.getElementsByTagName("input")
            If html_element.Type = "submit" Then html_element.Click: Exit For
        Next html_element
        
        Do Until ie.readyState = 4: DoEvents: Loop
       
    Err_Clear:
        If Err <> 0 Then Err.Clear
        Resume Next
        
        '/ Releases memory
        Set ie = Nothing
    End Sub
    Thank you!

    HC
    Last edited by hcyeap; 05-26-2016 at 12:53 AM. Reason: Solved

+ 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. Load New Picture with a click of the "Command Click()" button
    By Brainwave in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-13-2016, 01:49 PM
  2. VBA to Automate (Open web page, Select Dropdown, click button, fetch data in excel)
    By Mumbaimiraroad304 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2015, 09:17 AM
  3. [SOLVED] Use VBA to Automate Actions on Webpage (Open web page, Select Dropdown, click button)
    By ravikiran in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-03-2015, 07:29 AM
  4. Replies: 4
    Last Post: 04-05-2014, 08:53 AM
  5. Toggle BackColor of labels in Userform through "click" and "double-click"
    By ahmerjaved in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2013, 02:08 PM
  6. Difference between manual "click" of activeX checkbox and coded "click"
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-23-2012, 10:51 AM
  7. Replies: 1
    Last Post: 05-13-2008, 01:36 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