如何在公共函数中实现Application.Caller

时间:2018-05-21 06:46:34

标签: excel vba internet-explorer web-scraping automation

以下函数采用F列(货币代码)中的值,并返回H列中的函数输出(相当于输入的20美元)。

问题是当我将方程式向下拖动到H列(从H2开始)时,方程式不会更新F列中的值。

示例:H2 = Oanda(F2)
当我向下拖动时(比如F10),无论F3-F10的值是多少,等式都给出了Oanda(F2)的输出。我需要一些帮助来实现Application.Caller功能。

Option Explicit

Public Function Oanda(ConvertWhat As String)

'References
'   Microsoft XML, vs.0
'   Microsoft Internet Controls
'   Microsoft HTML Object Library

Dim Conversion As String
Conversion = ThisWorkbook.Sheets("Employee").Range("F" & ActiveCell.Row).Value2

Dim IE As New InternetExplorer
'IE.Visible = True
IE.Navigate "https://www.oanda.com/currency/converter?quote_currency=USD&base_currency=" & Conversion

Do
    DoEvents
Loop Until IE.ReadyState = ReadyState_Complete
Dim Doc As HTMLDocument
Set Doc = IE.Document
Dim Ans As String
Ans = Trim(Doc.getElementsByTagName("tbody")(2).innerText)
Dim AnsExtract As Variant
AnsExtract = Split(Ans, " ")

Oanda = AnsExtract(4) * 20

End Function

1 个答案:

答案 0 :(得分:0)

使用传递给函数的变量

Option Explicit

Public Function Oanda(ByVal ConvertWhat As String) As Double

    'References
    '   Microsoft XML, vs.0
    '   Microsoft Internet Controls
    '   Microsoft HTML Object Library
    Dim IE As New InternetExplorer
    'IE.Visible = True
    IE.Navigate "https://www.oanda.com/currency/converter?quote_currency=USD&base_currency=" & ConvertWhat

    Do
        DoEvents
    Loop Until IE.ReadyState = ReadyState_Complete
    Dim Doc As HTMLDocument
    Set Doc = IE.Document
    Dim Ans As String
    Ans = Trim$(Doc.getElementsByTagName("tbody")(2).innerText)
    Dim AnsExtract As Variant
    AnsExtract = Split(Ans, " ")

    Oanda = AnsExtract(4) * 20

End Function