在Visual Basic for Applications(VBA)中设置全局脚本.Dictionary

在Visual Basic for Applications(VBA)中设置全局脚本.Dictionary,vba,dictionary,Vba,Dictionary,我有一个附加字典的函数。只要updateList不正确,我就想保留这本词典的内容。我目前有如下设置: Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean) Dim dicTitle As Variable Dim dicFound As Variable If updateList = True Then Set dicTitle = CreateObject("Scripting.Dictionar

我有一个附加字典的函数。只要
updateList
不正确,我就想保留这本词典的内容。我目前有如下设置:

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)

Dim dicTitle As Variable
Dim dicFound As Variable

If updateList = True Then
Set dicTitle = CreateObject("Scripting.Dictionary")
Set dicFound = CreateObject("Scripting.Dictionary")

While status
        Set ObjectsJSON = jsonlibPollux.parse(responseString)

        With dicTitle
        .Add inc, ObjectsJSON.Item("title")
        End With

        With dicFound
        .Add inc, ObjectsJSON.Item("description")
        End With

Wend
End If
voerUit
为真时,会发生以下情况:

For i = 1 To dicTitle.Count
klaar = findReplace(dicTitle.Item(i), "x" + dicTitle.Item(i), dicTitle.Item(i) + vbCrLf + vbCrLf + dicFound.Item(i))
Next i
这里的问题是,当这个函数结束时,
dictle
dicFound
被清除,而
findReplace
函数得到空参数


是否有任何方法可以使代码正常工作,或者有一个好的解决方法

您需要添加对Microsoft脚本运行时的引用,才能像我使用的那样使用绑定。它比您声明它们的方式更好,因为您可以更好地访问对象,并且可以更容易地查看成员变量,等等

方法1-字典的模块级或公共变量 一种方法是为正在使用的脚本字典创建模块级变量:

Public gDicTitle As Scripting.Dictionary
Public gDicFound As Scripting.Dictionary

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)


    If updateList = True Then
        Set gDicTitle = New Scripting.Dictionary
        Set gDicFound = New Scripting.Dictionary
        
        While Status
                Set ObjectsJSON = jsonlibPollux.Parse(responseString)
        
                With gDicTitle
                .Add inc, ObjectsJSON.Item("title")
                End With
        
                With gDicFound
                .Add inc, ObjectsJSON.Item("description")
                End With
    
        Wend
    End If
End Function
方法2-字典的静态引用 你也可以通过制作字典来做到这一点。这将在函数调用之间保留它们,如文档所述

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)
    Static gDicTitle As Scripting.Dictionary
    Static gDicFound As Scripting.Dictionary
    
    If updateList = True Then
        Set gDicTitle = New Scripting.Dictionary
        Set gDicFound = New Scripting.Dictionary
        
        While Status
                Set ObjectsJSON = jsonlibPollux.Parse(responseString)
        
                With gDicTitle
                .Add inc, ObjectsJSON.Item("title")
                End With
        
                With gDicFound
                .Add inc, ObjectsJSON.Item("description")
                End With
    
        Wend
    End If
End Function

当我使用
Scripting.Dictionary
(静态或无)设置Dictle时,我收到一个编译错误,表示类型未定义(很抱歉,我需要进行荷兰式安装,因此我必须翻译错误消息)。@MartijnNosyncerror-您必须在VBA编辑器中添加对该库的引用,添加英文版的Microsoft脚本运行时库。我把它添加到我的答案的顶部。这就解决了这个错误,问题是我的字典在
findReplace
函数中使用时仍然是空的。我检查过了,两个字典都填充了第一个snippit,但当
循环结束时仍然会被删除。@MartijnNosyncerror请发布函数的其余代码,就像您在处理问题时使用它一样,看起来你的runPollufFull函数中的代码肯定比我在这里使用的要多。是的,我有,但这是很多垃圾,与问题无关。我会把它贴在pastebin上,因为它的大小,但大部分都是荷兰语(还有糟糕的编程,这是最糟糕的部分:D)