VBA Word数组 - 比较项目的最快方法

时间:2017-03-25 14:52:17

标签: arrays vba

可能以前曾经问过这个问题,但如果是这样的话,我找不到搜索它的方法。在那种情况下,道歉。

在Word VBA中,我正在寻找最快的方法来比较数组中的所有项目与之前的所有项目。由于这些很多,我想优化。 (这将是内存中的程序化数组,而不是像电子表格或表格或其他类似的数据。)

要比较的元素是长度相同的短字符串,如“0012”或“1023”。因为它们代表数字,所以第一个问题:

比较字符串或首先将项目转换为整数,然后进行比较是否更快?当然,为了做到这一点,还必须计算将字符串转换为数字的时间。

每次将新项目添加到数组时,都必须进行比较。几千次。第二个问题:

是否可能有算法执行此操作(例如,可以使用冒泡排序进行排序)?

非常感谢任何帮助。 最好, 迪特

1 个答案:

答案 0 :(得分:1)

OP评论后

编辑

此代码可以帮助您开始Dictionary方式:

Sub minna()
    Dim vals As Variant, val As Variant
    Dim dict As Dictionary

    vals = Array("0012", "1023", "0013", "1023", "0014", "0012") '<--| fill your array

    Set dict = New Dictionary '<--| instantiate a dictionary object
    With dict '<--| reference your dictionary object
        For Each val In vals '<--| loop through array
            .item(val) = .item(val) + 1 '<--| fill dictionary with unique values (duplicates are automatically discharged)
        Next

        For Each val In .Keys '<--| loop through dictionary keys. i.e. array unique values
            Debug.Print "Item " & val & " has " & .item(val) & "occurrences" '<--| show haow many occurrences of every array value
        Next
    End With
End Sub

要使用Dictionary对象,您必须将其库添加到项目引用中:工具 - &gt;引用 - &GT;滚动列表框直到&#34; Microsoft Scripting Runtime&#34;,将其复选标记切换为选中并单击&#34;确定&#34;按钮

处理2D数组你可以使用这个助手Function

Function GetDictionaryFromArray(vals As Variant) As Dictionary
    Dim dict As Dictionary
    Dim iVal As Long

    Set dict = New Dictionary
    With dict '<--| reference your dictionary object
        For iVal = 1 To UBound(vals) '<--| loop through passed array
            .item(vals(iVal, 1)) = vals(iVal, 2) '<--| fill dictionary with unique values (duplicates are automatically discharged)
        Next
    End With

    Set GetDictionaryFromArray = dict
End Function

你可以在你的&#34; main&#34;代码如下:

Sub Main ()
    Dim vals1(1 To 6, 1 To 2) As Variant, vals2(1 To 4, 1 To 2) As Variant, val As Variant, key As Variant
    Dim dict1 As Dictionary, dict2 As Dictionary
    Dim iVal As Long

    vals1(1, 1) = "0012": vals1(1, 2) = "a"
    vals1(2, 1) = "1023": vals1(2, 2) = "b"
    vals1(3, 1) = "0013": vals1(3, 2) = "c" '<--| this will survive in the conversion to dictionary
    vals1(4, 1) = "1023": vals1(4, 2) = "d" '<--| this will survive in the conversion to dictionary
    vals1(5, 1) = "0014": vals1(5, 2) = "e" '<--| this will survive in the conversion to dictionary
    vals1(6, 1) = "0012": vals1(6, 2) = "f" '<--| this will survive in the conversion to dictionary

    vals2(1, 1) = "0012": vals2(1, 2) = "a" '<--| this will survive in the conversion to dictionary
    vals2(2, 1) = "1023": vals2(2, 2) = "b"
    vals2(3, 1) = "0013": vals2(3, 2) = "c" '<--| this will survive in the conversion to dictionary
    vals2(4, 1) = "1023": vals2(4, 2) = "d" '<--| this will survive in the conversion to dictionary

    Set dict1 = GetDictionaryFromArray(vals1) '<--| make a 'Dictionary' object out of first 2D array
    Set dict2 = GetDictionaryFromArray(vals2) '<--| make a 'Dictionary' object out of second 2D array

    With dict1 '<--| reference your first dictionary object
        For Each key In .Keys '<--| loop through referenced dictionary keys. i.e. first array column 1 unique values
            If dict2.Exists(key) Then '<--| if second dictionary has the same key
                Debug.Print "Item (" & key & "," & .item(key) & ") in first array has the following corresponding item in second: (" & key & "," & dict2(key) & ")" '<--| show how many occurrences of first dictionary keys in the second dictionary
            End If
        Next
    End With
End Sub