无法循环浏览字典键

时间:2019-07-09 14:45:16

标签: excel vba dictionary

为了工作,我正在创建一个字典,该字典从excel表格中获取信息,该表格包含多个产品代码以及生产日期及其对应的数量。字典的目标是将所有相同的代码编译为1行,并将数量加在一起为1行。例如,这就是我的数据当前的样子

               MON    TUES    WED    THURS    FRI
product 1              3       1               5
product 2       5              4       2
product 2       4              4       1
product 3              8                   
...
product n

这就是我想要代码格式化的方式,

               MON    TUES    WED    THURS    FRI
product 1       2      3       4              5  
product 2       9              8       3
product 3              8                   
...
product n

为了实现这一点,我将多个值存储在一个键中。关键是产品代码,生产日期是项目。为了在键中存储多个值,我使用了一个类(使用this site作为指导)。我大量使用了该站点及其示例,作为指导,我之前没有做过此操作。

我遇到的问题是,每次尝试打印字典时,无论是Debug.Print还是工作表,都会出现“ 424对象为必需”错误。我不确定为什么会出现此错误,因为我什至无法检查字典是否正常工作。

我查看了多个帖子(12)和网站,以尝试使用不同的方法遍历关键字,以查看它们是否会产生不同的结果(但没有)

这是我的一些代码:

Option Explicit
Sub concatenateData()

    Dim dictPC As Object
    Dim productCodes As clsProductCodes
    Dim wsCryovac As Worksheet
    Dim PLU As Integer
    Dim Friday As Integer
    Dim Saturday As Integer
    Dim Monday As Integer
    Dim Tuesday As Integer
    Dim Wednesday As Integer
    Dim Thursday As Integer
    Dim row As Long
    Dim col As Long
    Dim lrow As Long

    Set dictPC = CreateObject("Scripting.Dictionary")
    Set wsCryovac = ThisWorkbook.Sheets("Cryovac")
    Set Sheet2 = ThisWorkbook.Sheets("Sheet2")
    lrow = wsCryovac.Cells(Rows.Count, 1).End(xlUp).row
    For row = 4 To lrow

        PLU = wsCryovac.Cells(row, 1).Value2
        Friday = wsCryovac.Cells(row, 2).Value2
        Saturday = wsCryovac.Cells(row, 3).Value2
        Monday = wsCryovac.Cells(row, 4).Value2
        Tuesday = wsCryovac.Cells(row, 5).Value2
        Wednesday = wsCryovac.Cells(row, 6).Value2
        Thursday = wsCryovac.Cells(row, 7).Value2

        If dictPC.Exists(PLU) = True Then
            Set productCodes = dictPC(PLU)
        Else
            Set productCodes = New clsProductCodes
            dictPC.Add PLU, productCodes
        End If

        dictPC(productCodes.PLU) = PLU
        dictPC(productCodes.Friday) = dictPC(productCodes.Friday) + Friday
        dictPC(productCodes.Saturday) = dictPC(productCodes.Saturday) + Saturday
        dictPC(productCodes.Monday) = dictPC(productCodes.Monday) + Monday
        dictPC(productCodes.Tuesday) = dictPC(productCodes.Tuesday) + Tuesday
        dictPC(productCodes.Wednesday) = dictPC(productCodes.Wednesday) + Wednesday
        dictPC(productCodes.Thursday) = dictPC(productCodes.Thursday) + Thursday

    Next row

    WriteToImmediate dictPC

End Sub
'This is where I attempt to check the dictionary information with code I used from: 
'https://excelmacromastery.com/vbadictionary/#Example_2_8211_Dealing_with_Multiple_Values
Private Sub WriteToImmediate(dictPC As Dictionary)

    Dim key As Variant, productCodes As clsProductCodes

    ' Read through the dictionary
    For Each key In dictPC.Keys
        Set productCodes = dictPC(key) 'I get the 424 error on this line
        With productCodes
            ' Write to the Immediate Window (Ctrl + G)
            Debug.Print .PLU, .Friday, .Saturday, .Monday, .Tuesday, .Wednesday, .Thursday
        End With
    Next key

End Sub

重申一下,我遇到的问题是我收到“ 424 object error”,我不确定该如何解决,部分原因是由于我缺乏使用字典的经验。

1 个答案:

答案 0 :(得分:2)

我在上面说错了-您的字典中确实有Id ParentId CategoryName -----+-------------+----------------- 1 | NULL | CatA -----+-------------+----------------- 2 | 1 | CatB -----+-------------+----------------- 3 | 2 | CatC -----+-------------+----------------- 4 | 3 | CatD -----+-------------+----------------- 的实例:

clsProductCodes

但是您可以这样做:

Set productCodes = New clsProductCodes
dictPC.Add PLU, productCodes

这里您不更新存储在字典中的对象-您要添加新的键和值

认为您打算做更多这样的事情:

    dictPC(productCodes.PLU) = PLU
    dictPC(productCodes.Friday) = dictPC(productCodes.Friday) + Friday
    dictPC(productCodes.Saturday) = dictPC(productCodes.Saturday) + Saturday