来自另一个数组的键的字典值

时间:2019-04-12 17:03:03

标签: arrays excel vba dictionary

此问题已得到解答...必须等待2天才能接受自己的答案


背景

我有一个从Excel中的数据集生成的数组(“ arr”);我使用该数组填充另一个数组(“ zrr”),该人口的一个方面是使用字典(“ dcdept”)。

已适当地填充了字典(通过debug.print dcdept(ActualKey)进行了测试;已填充,使得dcdept(4000)="Value"和经过测试的debug.print dcdept(4000)在立即窗口中显示了“值”一词。

我本来是通过.cells(i,)引用来使用源数据集的,但是由于有几十万行,我试图将活动保留在VBA中以加快速度。

我的代码没有生成错误/警报。


问题:

当尝试使用arr(zrr(i-1,3))中的字典键在zrr(dcdept(arr(i-2,16)))中填充元素时,没有任何输出值。


问题:

有人对使用给定数据解决问题有任何建议/解决方案吗?


相关代码:

Public arr As Variant, brr As Variant, crr As Variant, drr As Variant, lrs As Long
Private Sub changes()
    Dim i As Long, x As Long, y As String, z As String, dcdept As Scripting.Dictionary, zrr As Variant, a As Long
    'set-up dictionary for department
    Set dcdept = New Scripting.Dictionary
    dcdept(4000) = "Value"
    'generate array to store new values
    With Sheets("Conversion")
        .Columns(16).NumberFormat = "0"
        lrs = .Cells(.Rows.Count, 1).End(xlUp).Row
        arr = .Range(.Cells(2, 1), .Cells(lrs, 17)).Value '17 = Q
        ReDim zrr(lrs, 4)
        For i = 2 To lrs
            ReDim Preserve zrr(lrs, 4)
            Select Case Left(arr(i - 1, 17), 3)
                Case "QTE"
                    x = 7
                Case "ZNA"
                    x = 5
            End Select
            zrr(i - 2, 0) = Right(arr(i - 1, 17), x)
            If InStr(arr(i - 1, 9), " Milestone ") Then
                y = Left(arr(i - 1, 9), 2) & " " & arr(i - 1, 10)
            Else
                y = arr(i - 1, 9) & " " & arr(i - 1, 10)
            End If
            zrr(i - 2, 1) = y
            If IsEmpty(arr(i - 1, 14)) Then
                zrr(i - 2, 2) = "N"
            Else
                zrr(i - 2, 2) = "Y"
            End If
            a = Val(arr(i - 1, 16))
            z = dcdept(a)
            zrr(i - 2, 3) = z
            Debug.Print a
            Debug.Print z
        Next i
        'append data to sheet
        .Cells(2, "R").Resize(lrs, 3).Value = zrr  'SHOULD BE Resize(lrs,4), per answer
    End With
End Sub

2 个答案:

答案 0 :(得分:0)

好的,这不是答案,而是我的评论的例证。我没想到会发生这种情况。我设置了一个简单的场景,希望与您相似:

Sub x()

Dim oDic As Object, v1(1 To 2), v2(1 To 2), v, i As Long

Set oDic = CreateObject("Scripting.Dictionary")

v1(1) = "Fred"
v1(2) = 1000

oDic(1) = v1(1) 'key 1, item "Fred
oDic(2) = v1(2) 'key 2, item 1000

此后的“本地人”窗口如下

enter image description here


然后添加此行

v2(1) = oDic(v1(1))

,立即窗口显示为:

enter image description here


添加此行

v2(2) = oDic(v1(2))

,立即窗口显示为:

enter image description here

答案 1 :(得分:0)

我是个白痴...

.Cells(2, "R").Resize(lrs, 3).Value = zrr 

应该是

.Cells(2, "R").Resize(lrs, 4).Value = zrr

2天内无法接受我自己的答案;同时请原谅“未回答”的问题。