Mac上的VBA Excel Mac词典类Get Keys()属性

马特

使用适当大小的数据集,Get Keys()属性的速度很慢。有没有一种方法可以分配键而不循环遍历每个记录?

我只能找到for或do循环分别分配键的示例。


Public Property Get Keys() As String()

'Here is the existing code (status bar updated only so the user knows the
'program is still working).

    Dim vlist() As String, i As Long, myKey As Variant, myArr() As Variant, okVP As KeyValuePair, StatBar As String
    StatBar = Application.StatusBar

    If Me.Count > 0 Then
        ReDim vlist(0 To Me.Count - 1)        

        For i = LBound(vlist) To UBound(vlist)
            Set okVP = KeyValuePairs(i + 1)
            vlist(i) = okVP.Key
            If i Mod 5000 = 0 Then
                Application.StatusBar = StatBar & ": " & "Gathering Keys: " & Format(i, "#,##0") & " of " & Format(UBound(vlist), "#,##0")
'                Debug.Print i & " of " & UBound(vlist)
                DoEvents
            End If
        Next i
        Keys = vlist
    End If
    Application.StatusBar = StatBar
End Property

目前的方法需要大约5分钟的时间才能获得约135,000条记录。

马修·金登(Mathieu Guindon)

不,您无法避免迭代这些项目。在Windows下,您可能会利用CopyMemoryWin32 API来批量复制阵列,但是我不相信这些功能可以在Mac上运行。

考虑你的交换KeyValuePair为基础的Dictionary添大厅的Drop-inDictionary替代

现在,很难看到类的[_NewEnum]实现和默认成员(您有那些吗?),但是据我所知,您正在使用循环迭代对象集合For...Next-这是迭代任何对象的最慢方法采集。

对象集合使用进行迭代For Each...Next那样,就可以将运行时间减少几个数量级

更新ApplicationUI并运行DoEvents(甚至每5K迭代运行一次)会进一步减慢循环速度……而且,Property Get无论如何,这都不是您想要在成员中执行的操作。

看起来KeyValuePairsKeyValuePair对象集合吗?其中的135K意味着很多开销-在.NET中,aKeyValuePairstruct(值类型),而不是object(引用类型);它们进行了不同的优化,并且性能也有所不同。无论如何,如果您有KeyValuePair对象集合,则迭代它们的最快方法是使用For Each循环。

ReDim result(0 To KeyValuePairs.Count - 1)
Dim i As Long
Dim kvp As KeyValuePair
For Each kvp In KeyValuePairs
    result(i) = kvp.Key
    i = i + 1
Next

此外,由于数组在VBA中的工作方式以及与类型化数组相关的所有麻烦,我认为如果将指针包装在指针中或通过参数“返回”,返回一个数组会更安全(但这对成员不起作用))。VariantByRefProperty Get

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章