vba"。删除键"方法只将Empty放到相应的Dictionary项

时间:2015-12-04 11:20:15

标签: excel vba dictionary key

我有一个固定词典,一个包含固定信息的自定义类:

master as long
slave as long
model as string
mcomp as collection
scomp as collection

密钥(fixationkey as string)使用:fixation.master & fixation.slave & fixation.model & mcomps构建(mcomps是所有向量组件的字符串,通过此固定组合在一起)。 看来这本词典中的一些注意事项具有相同的主,同一个从属和相同的模型。只有组件不同。我想要做的是将所有组件重新组合在一起。 所以我实现了这个方法:

Sub detectfixationsdoubles()
''Detect fixation that are similar: same master node, same slave node, same model but with different comps
''Merge these fixations
    Dim fixationkey as string
    Dim fixationcompared As Variant
    Dim fixationcomparedto As Variant
    Dim newfixation As fixation
    Dim compitem As Variant

    'First loop on the fixations: each fixation is compared
    For Each fixationcompared In Cfixationstreat.Items
        'Second loop on the fixations : each fixation is compared to all the fixations
        For Each fixationcomparedto In Cfixationstreat.Items
            If fixationcompared.master = fixationcomparedto.master _
                    And fixationcompared.slave = fixationcomparedto.slave _
                    And fixationcompared.model = fixationcomparedto.model Then
                If Not fixationcompared Is fixationcomparedto Then
                    Set newfixation = New fixation
                    Set newfixation = fixationcompared

                    With fixationcompared
                        fixationkey = .master & .slave & .model
                        For Each compitem In .mcomp
                            fixationkey = fixationkey & compitem
                        Next
                    End With

                    Cfixationstreat.Remove fixationkey
                    fixationcompared = Empty

                    With fixationcomparedto
                        For Each compitem In .mcomp
                            newfixation.mcompadd compitem
                        Next
                        For Each compitem In .scomp
                            newfixation.scompadd compitem
                        Next

                        fixationkey = .master & .slave & .model
                        For Each compitem In .mcomp
                            fixationkey = fixationkey & compitem
                        Next
                    End With

                    Cfixationstreat.Remove (fixationkey)
                    fixationcomparedto = Empty

                    With newfixation
                        fixationkey = .master & .slave & .model
                        For Each compitem In .mcomp
                            fixationkey = fixationkey & compitem
                        Next
                    End With

                    Cfixationstreat.Add fixationkey, newfixation
                    Exit For
                End If
            End If
        Next
    Next
End Sub

然而,我看到的是我要删除的2个注意事项仍然在词典中。密钥仍然存在,但与Empty相关联。 这是excel的问题(我使用excel 2007)?这是一个问题,因为对象(我还有指针问题)?我有没有语法错误?

感谢您的帮助。

PS:如果你看到任何方法来提高这种方法的速度,它会有所帮助。现在它耗费时间,即使是中等大小的词典......

1 个答案:

答案 0 :(得分:0)

我的坏!对不起那些试图找到答案的人。我有一个间谍:Cfixationtreat(固定键)。 我相信这就是为什么关键不能全部删除。