本文介绍了通过 VBA 从网页上的下拉列表中选择一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 VBA 代码来在 XE 货币转换器网站上转换货币并给我结果.我不知道如何从列表中选择货币.下面附上我的代码:

I want a VBA code to convert currency on XE currency converter site and give me result. I am not getting how to select a currency from list. Below is attacched my code:

Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer

ie.Visible = True
ie.navigate "http://www.xe.com/currencyconverter/"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "loading web page...."
DoEvents
Loop
Set html = ie.document
Dim drp As HTMLFormElement
html.getElementById("amount").Value = 100
'html.getElementById("from").Value = "Euro"
Set drp = html.getElementById("from_sc")
drp.Click

推荐答案

试试这个,如果我错过了什么,请告诉我.根据需要更改包含 GBP - British PoundEUR - Euro 的字段.

Give this a shot and let me know if I've missed anything. Change the fields containing GBP - British Pound or EUR - Euro according to your need.

Sub Get_Result()
    Dim IE As New InternetExplorer, html As HTMLDocument
    Dim post As Object, elem As Object

    With IE
        .Visible = True
        .navigate "http://www.xe.com/currencyconverter/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    html.getElementById("amount").Value = 100
    html.getElementById("from_sc").Click

    For Each post In html.getElementsByClassName("ac_odd ac_recent")
        If InStr(post.innerText, "EUR - Euro") > 0 Then post.Click: Exit For
    Next post

    html.getElementById("to_sc").Click

    For Each elem In html.getElementById("to_scroller").getElementsByClassName("ac_even ac_recent")
        If InStr(elem.innerText, "GBP - British Pound") > 0 Then elem.Click: Exit For
    Next elem

    html.getElementById("ucc_go_btn_svg").Click
End Sub

这篇关于通过 VBA 从网页上的下拉列表中选择一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:14