代码之家  ›  专栏  ›  技术社区  ›  Aaron Parisi

如何引用字典中的数组项?

  •  0
  • Aaron Parisi  · 技术社区  · 5 年前

    我试图确定VBA字典中任何一个数组项中的任何一个字符串是否等于4个空格的字符串。

        If _
        Not CStr(info.Items(1, 4)) = "    " Or _
        Not CStr(info.Items(1, 5)) = "    " Or _
        Not CStr(info.Items(1, 6)) = "    " Or _
        Not CStr(info.Items(2, 4)) = "    " Or _
        Not CStr(info.Items(2, 5)) = "    " Or _
        Not CStr(info.Items(2, 6)) = "    " Or _
        Not CStr(info.Items(3, 4)) = "    " Or _
        Not CStr(info.Items(3, 5)) = "    " Or _
        Not CStr(info.Items(3, 6)) = "    " Or _
        Not CStr(info.Items(4, 4)) = "    " Or _
        Not CStr(info.Items(4, 5)) = "    " Or _
        Not CStr(info.Items(4, 6)) = "    " Then
    

    我不断得到一个下标超出范围的错误。我试过了

    ...info.Items(1)(4)... 同样的错误。

    我知道每个数组项中有6个元素,我知道字典中有4个键。如果项是数组,如何访问每个键项的元素?

            Dim RQItems As Dictionary
            Dim RPItems As Dictionary
            Dim IMPItems As Dictionary
            Dim EMItems As Dictionary
            Dim BOOTItems As Dictionary
    
            Dim RQ1(6) As String
            Dim RQ2(6) As String
            Dim RQ3(6) As String
            Dim RQ4(6) As String
    
    
    
    
            Set RQItems = New Dictionary
    
            RQ1(1) = "PSA "
            RQ1(2) = "Prlm"
            RQ1(3) = "Info"
            RQ1(4) = "    "
            RQ1(5) = "    "
            RQ1(6) = "    "
    
            RQ2(1) = "Mary"
            RQ2(2) = "Clnt"
            RQ2(3) = "Escr"
            RQ2(4) = "Bank"
            RQ2(5) = " SS "
            RQ2(6) = "    "
    
            RQ3(1) = "Inst"
            RQ3(2) = "Wire"
            RQ3(3) = "    "
            RQ3(4) = "    "
            RQ3(5) = "    "
            RQ3(6) = "    "
    
            RQ4(1) = "Acct"
            RQ4(2) = "Fee "
            RQ4(3) = "    "
            RQ4(4) = "    "
            RQ4(5) = "    "
            RQ4(6) = "    "
    
            RQItems("OPEN") = RQ1
            RQItems("DOCS") = RQ2
            RQItems("$$$$") = RQ3
            RQItems("FILE") = RQ4
    
    
    

    我把它们传递到一个函数中,比如 myFn(info As Dictionary)

    0 回复  |  直到 5 年前
        1
  •  1
  •   Tim Williams    5 年前

    Sub Test()
    
        Dim dict As New Dictionary, arr(1 To 4), k, arr2, v
    
        arr(1) = "One"
        arr(2) = "Two"
        arr(3) = "Three"
        arr(4) = "four"
    
        dict.Add "Test", arr 
    
        'access a single item
        Debug.Print dict("Test")(1) '>> One
    
    
        'loop over all contained arrays 
        For Each k In dict.Keys
            arr2 = dict(k)
    
            Debug.Print arr2(3) 'access a single element
    
            'or loop through all elements in the array
            For Each v In arr2
                Debug.Print k, v
            Next v
        Next k
    
    End Sub