使用VLOOKUP作为VLOOKUP中的查找值的Excel导致值错误

时间:2018-06-14 14:29:20

标签: excel excel-vba excel-formula vlookup excel-2016 vba

我有以下excel公式:

=IF([@Article]="";"";IF(VLOOKUP([@Article];Articles[#All];9;FALSE)="";"";VLOOKUP([@Article];Articles[#All];9;FALSE)*wGetConversionRate(VLOOKUP(VLOOKUP([@Article];Articles[#All];10;FALSE);Countries[#All];4;FALSE))))

现在,它非常满口,但其中有一些逻辑。前两个IF只是检查单元格是否已填入。

此公式的输出将是文章的成本,从当地货币转换为欧元。函数wGetConversionRate是一个UDF,用于从网站(AlphaVantage)获取最新的转换率。问题是Articles表中的本地货币不在国际代码中,这意味着我必须在另一个VLOOKUP中使用VLOOKUP作为查找值。 '嵌套' VLOOKUP VLOOKUP([@Article];Articles[#All];10;FALSE)获取当地货币,例如'印度卢比'。然后,上面的VLOOKUP VLOOKUP(*nested VLOOKUP*;Countries[#All];4;FALSE应该返回给定货币的正确国际代码,例如,    为印度卢比'。相反,它返回#VALUE!,所以我的猜测是,返回字符串的嵌套VLOOKUP不能用作查找值。可能有些事我做错了,还是以其他方式解决这个问题?

UDF具有以下代码:

Function wGetConversionRate(currencyFrom As String) As Double
' Variables
Dim currencyTo As String
currencyTo = "EUR"
Dim functionAPI As String
Dim from_currencyAPI As String
Dim to_currencyAPI As String
Dim keyAPI As String
Dim stringAPI As String
Dim httpREQ As New WinHttp.WinHttpRequest
Dim jsonString As String
Dim splitJSONString() As String
Dim exchangeRateString As String
Dim splitExchangeRateString() As String
Dim exchangeRate As Variant

' API String
functionAPI = "CURRENCY_EXCHANGE_RATE"
from_currencyAPI = currencyFrom
to_currencyAPI = currencyTo
keyAPI = "33VEUWOLZGXE1HA9"
stringAPI = "https://www.alphavantage.co/query?function=" + functionAPI + "&from_currency=" + from_currencyAPI + "&to_currency=" + to_currencyAPI + "&apikey=" + keyAPI

' Get JSON string
httpREQ.Open "GET", stringAPI
httpREQ.Send
jsonString = httpREQ.ResponseText

' Split JSON string into usable parts (put into array)
splitJSONString = Split(jsonString, ",")

' Get Exchange Rate item from splitJSONString
exchangeRateString = splitJSONString(4)

' Split exchangeRateString into usable strings (into array)
splitExchangeRateString = Split(exchangeRateString, ":")

' Get exchange rate from splitExchangeRateString
exchangeRateString = Replace(splitExchangeRateString(1), """", "")
exchangeRateString = Replace(exchangeRateString, ".", ",")
exchangeRate = CDec(exchangeRateString)

' Output the exchange rate
wGetConversionRate = exchangeRate
End Function

然后来自公式评估的信息给我的是,当计算嵌套的VLOOKUP时,它返回一个字符串,这似乎在上面的VLOOKUP中给出了一个问题,因为当它试图查找时使用字符串作为查找值的货币,它返回#VALUE!

0 个答案:

没有答案