立即显示变体数组值 Window

Displaying a Variant Array Value in the Immediate Window

我正在使用第三方加载项,它似乎工作正常。但是,我无法立即显示变体数组元素的值 window。我已经找到解决方法,但我仍然想知道问题是什么以及如何解决它。

有一个包含数据的对象变量。

Dim odsDataSeries As DataSeries
Set odsDataSeries = odfData.GetSeries("ELY(high)")

在 Watch Window 中,您可以看到名为 "Index" ...

的变体数组

我立即 window 输入...

?odsDataSeries.Index(1)

但它给出了错误消息 "Wrong number of arguments or invalid property assignment"

如果我使用 join 然后我得到数据...

?join(odsDataSeries.Index)
3/01/2020 2/01/2020 31/12/2019 etc... 

在我的代码中,我还能够编写...

Dim v As Variant
v = odsDataSeries.Index
Stop

然后当我在 Immediate Window 中查询 v 中的元素时,它起作用了...

?v(1)
2/01/2020 

所以我的问题是...为什么我的即时 Window 查询“?odsDataSeries.Index(1)”不起作用?有一段时间我认为 Index 不是 public 并且我无法编写循环来处理数组(我仍然不能,除非我将数组放在另一个变量中,如上所示)。请注意,在我的代码中编写此代码会给出相同的错误消息(在编译时)。

我正在添加我的整个子以供参考。请注意,我使用了早期绑定。 excel加载项是一个.xll文件,我看不到里面的代码(因为我还没有COM技能)。

Dim av As New AlphaVantageExcelDataCOMFunctions

Public Sub PublicLoadData()
On Error GoTo 0
Dim odfData As DataFrame
    Set odfData = av.AVGetEquityTimeSeries("ELY", "Daily", True, "compact")
Dim odsDataSeries As DataSeries
    Set odsDataSeries = odfData.GetSeries("ELY(high)")
Dim v As Variant
    v = odsDataSeries.Index
    Debug.Print GetVariableType(odsDataSeries.Index)
    Stop
End Sub 'PublicLoadData

我的 GetVariableType() 函数 returns "Array of Variant"。如果您需要此代码,请告诉我。

所以问题又是...在立即 window 我键入...

?odsDataSeries.Index(1)

但它给出了错误消息 "Wrong number of arguments or invalid property assignment"

如果我使用 join 然后我得到数据...

?join(odsDataSeries.Index)
3/01/2020 2/01/2020 31/12/2019 etc... 

当我在立即 Window 中查询 v 中的元素时,它有效...

?v(1)
2/01/2020 

Here is a link to the DataSeries Documentation

假设有一个对象 someObject 有一个方法(或 属性)getArray 没有输入参数并返回一个数组。

您可以通过 arr = someObject.getArray 检索数组,这与 arr = someObject.getArray() 相同,没有显式传递参数。要通过一行代码从该数组中检索单个元素,您应该执行 someObject.getArray()(0),其中第一个括号表示方法调用,第二个表示数组中元素的索引。

注意,虽然表达式someObject.getArray()(i)适用于单元素请求,e。 G。 in Immediate window,但如果在循环中使用,可能会导致每次迭代检索数组的大量开销。所以你应该在循环之前将数组放入变量中。

下面的示例显示了常用 Dictionary 对象的一些棘手行为。您可以简单地通过 dict.Keys(i) 使用早期绑定从方法返回的键数组中检索元素:

Sub testEarlyBoundDict()

    ' Need to include a reference to "Microsoft Scripting Runtime"

    Dim earlyBoundDict As Dictionary
    Set earlyBoundDict = New Dictionary
    Set earlyBoundDict = CreateObject("Scripting.Dictionary")
    earlyBoundDict("myKey") = "myValue"
    Debug.Print earlyBoundDict.Keys(0)

End Sub

但是如果你使用后期绑定那么你必须写额外的括号:

Sub testLateBoundDict()

    ' Need no references

    Dim lateBoundDict As Object
    Set lateBoundDict = CreateObject("Scripting.Dictionary")
    lateBoundDict("myKey") = "myValue"
    Debug.Print lateBoundDict.Keys()(0)
    Debug.Print lateBoundDict.Keys(0) ' fails

End Sub

要创建防故障代码,我建议始终使用 someObject.getArray()(i)