我的字典无法正常运作

时间:2016-12-06 02:59:26

标签: excel vba dictionary

我正在使用字典对象对值进行排序和计数,并将它们打印到工作表中。还有人帮我编写代码,这对我来说似乎很像巫术。

在我的数据中,我有30个区块,每个区块包含18个试验。在30个区块之前我还有1个练习块,在30个区块之后还有10个传输块。所有代码都是使用不包含传输块的数据样本编写的,但我打算将其他数据文件复制/粘贴到包含传输块的原始文件中。

不知何故,我的代码知道忽略了练习块(这就是我想要的),但是当我复制/粘贴数据时,它也忽略了传输块(这不是我想要的)。我不知道为什么代码表现得像这样。

以下是字典对象的代码:

Dim dBT As Object 'global dictionary

Sub buttonpresscount()

    'constants for column positions
    Const COL_BLOCK As Long = 1
    Const COL_TRIAL As Long = 2
    Const COL_ACT As Long = 7
    Const COL_AOI As Long = 8
    Const COL_RT As Long = 16

    Dim rng As Range, lastrow As Long, sht As Worksheet
    Dim d, r As Long, k, resBT()

    Set sht = Worksheets("full test")
    lastrow = Cells(Rows.Count, 3).End(xlUp).Row
    Set dBT = CreateObject("scripting.dictionary")

    Set rng = sht.Range("B7:T" & lastrow)

    d = rng.Value  'get the data into an array

    ReDim resBT(1 To UBound(d), 1 To 1) 'resize the array which will
                                        '  be placed in ColT

    'get unique combinations of Block and Trial and pressedcounts for each
    For r = 1 To UBound(d, 1)
        k = d(r, COL_BLOCK) & "|" & d(r, COL_TRIAL) 'create key
        dBT(k) = dBT(k) + IIf(d(r, COL_ACT) <> "", 1, 0)
    Next r

    'populate array with appropriate counts for each row
    For r = 1 To UBound(d, 1)
        k = d(r, 1) & "|" & d(r, 2)   'create key
        resBT(r, 1) = dBT(k)         'get the count
    Next r

    'place array to sheet
    sht.Range("T7").Resize(UBound(resBT, 1), 1) = resBT

    'clear dictionary
    dBT.RemoveAll

'count AOI entries
 For r = 1 To UBound(d, 1)
        k = d(r, COL_BLOCK) & "|" & d(r, COL_TRIAL) 'create key
        If resBT(r, 1) = 1 Then    'only proceed with trials with 1 button press
        dBT(k) = dBT(k) + IIf(d(r, COL_AOI) = "AOI Entry", 1, 0)    'get count
        Else: dBT(k) = ""
        End If
    Next r

    'populate array with appropriate counts for each row
    For r = 1 To UBound(d, 1)
        k = d(r, 1) & "|" & d(r, 2)   'create key
        resBT(r, 1) = dBT(k)          'get the count
    Next r

    'place array to sheet
    sht.Range("U7").Resize(UBound(resBT, 1), 1) = resBT

Call createsummarytable
Call PopSummaryAOI(dBT)

dBT.RemoveAll

'retrieve and print reaction times to data summary sheet
   For r = 1 To UBound(d, 1)
        If resBT(r, 1) <> "" Then
        k = d(r, COL_BLOCK) & "|" & d(r, COL_TRIAL) 'create key
        dBT(k) = d(r, COL_RT)
        Else: dBT(k) = ""
        End If
    Next r

 'Populate array with last row reaction time for each trial
        For r = 1 To UBound(d, 1)
        k = d(r, 1) & "|" & d(r, 2)   'create key
        resBT(r, 1) = dBT(k)          'get the count
    Next r

Call PopSummaryRT(dBT)

End Sub

Here is a screenshot of some of my data:

以下是摘要表的屏幕截图:

enter image description here

如何让代码仍然忽略练习块,但分析传输块?

我遇到的第二个问题是(如果您查看我的第二个屏幕截图,在第29块的第10个试验版中)即使为AOIentries打印了一个值,也不会打印某些反应时间值。

对于那些有兴趣查看示例数据和源代码以及子宏的人,here it is.

更新:我一直在尝试查找关于摘要表中缺少的反应时间值的模式 - 缺少的RT值仅出现在其他缺少的AOI条件和RT值的块中。也就是说,当块具有空白AOIentries和RT配对时,总会缺少另一个RT值。在没有空白AOIentries值的块中,没有丢失的RT值。因此,在特定块中将AOIentries单元格留空会导致多个RT值保留为空,但仅在该块中,而不是块中的每个RT值。

此外,缺少不应丢失的RT值始终来自试验前不久出现的试验,其中AOIentries值被排除(试验呈现在每个块中随机化)。例如。如果试验17有2个按钮按下,则试验17的AOIentries值和RT值将保持空白,但如果试验6是试验17之前的试验,它也将具有空白RT值。

2 个答案:

答案 0 :(得分:1)

这里:

  'retrieve and print reaction times to data summary sheet
  For r = 1 To UBound(d, 1)
        If resBT(r, 1) <> "" Then
            k = d(r, COL_BLOCK) & "|" & d(r, COL_TRIAL) 'create key
            dBT(k) = d(r, COL_RT)
        Else
            dBT(k) = ""
        End If
   Next r

在某些情况下,您将清除dBT(k)以前它具有AOI数据的值(您正在测试resBT(r, 1) = 1以决定是否在字典中清空该位置)。这就是在摘要数据中创造“差距”的原因。

关于您的“转移”试验,您的数据中包含以下标记:

Block               Trial  
Transfer Block 2    Transfer trial, 3
摘要表上的

是这样的:

Block               Trial  
Transfer Block 2    Trial, 3

因此当您尝试填充摘要

时它不匹配

将此更改更改为createsummarytable,数据将填充:

'print trial number headings
 For j = 1 To 18
     .Cells((Startrow + 1) + (5 * i), j).Value = IIf(i < 31, _
                                     "Trial, " & j, _
                                     "Transfer trial, " & j)
 Next j

答案 1 :(得分:0)

很难跟踪那里发生的事情

但您可能会尝试更改:

'count AOI entries
    For r = 1 To UBound(d, 1)
        k = d(r, COL_BLOCK) & "|" & d(r, COL_TRIAL) 'create key
        If resBT(r, 1) = 1 Then  'only proceed with trials with 1 button press
        dBT(k) = dBT(k) + IIf(d(r, COL_AOI) = "AOI Entry", 1, 0)    'get count
        Else: dBT(k) = ""
        End If
    Next r

为:

'count AOI entries
    For r = 1 To UBound(d, 1)
        k = d(r, COL_BLOCK) & "|" & d(r, COL_TRIAL) 'create key
        If resBT(r, 1) = 1 And Left(resBT(r, 1), 8) <> "Transfer" Then  'only proceed with trials with 1 button press and not from a "Transfer" block
        dBT(k) = dBT(k) + IIf(d(r, COL_AOI) = "AOI Entry", 1, 0)    'get count
        Else: dBT(k) = ""
        End If
    Next r