【问题标题】:using a dictionary and array to count cell values使用字典和数组计算单元格值
【发布时间】:2016-10-20 07:01:42
【问题描述】:

这是this question.的扩展我想做类似的事情,但是我对字典对象不是很熟悉,而且答案中提供的代码很高级,所以我很难理解。例如,一些语法不是很清楚,变量名不是很明显/直观。我正在创建一个新问题,因为原始问题已解决。

我想做与链接问题完全相同的事情,但我不想计算 H 列中的单元格值,而是想计算每次试验的 AOI 条目(并忽略退出)并在 I 列中阻止,并打印数字在 U 列中。

如果您还可以提供解决方案的解释以伴随您的解决方案(以便我了解发生了什么),我们将不胜感激。或者至少解释一下前面的解决方案中发生了什么。

Here is a link to my most up to date sample data and code.

【问题讨论】:

  • 这看起来是一个非常具体的解决方案,你到底想做什么?您可以在此处的图片/表格中发布您的示例数据吗?你会得到更好的回应
  • 很遗憾看不到您的屏幕打印,所以不完全理解这个问题。但是在您的另一篇文章中,使用 Tim 的代码,您能否更具体地说明您不理解代码的哪一部分?或许能帮上忙
  • @Zac 我附上了一个链接来下载这个问题中的文件。我也会用数据的截图来编辑问题。我不明白这一行的语法:'Dim d, r As Long, k, resBT()' 我知道 d 和 r 是计数器, resBT() 是数组,但是 k?我以前从未见过这样写的模糊声明。同样在这条线上苦苦挣扎:'dBT(k) = dBT(k) + IIf(d(r, COL_ACT) "", 1, 0)' 我没有得到参数,也从来没有遇到过 IIF 参数之前。
  • @tompreston 在我计算了每个块每次试验的按钮按下次数(这是上一个问题的内容)之后,我想从进一步分析中排除按下多个按钮的试验,我想要计算每个区块每次试验的 AOI 条目。
  • 首先是DIM 语句:VBA 将接受这种写DIM 语句的格式。我倾向于为每个变量写一个dim 语句,更多用于阅读目的。将其作为单独的 dim 语句阅读。所以在这种情况下,'k' 和 resBT() 都是 Variant 类型,因为它们没有声明。现在IIFhave a read of this article。本质上,IIF 评估所有 3 个参数,其中 IF 将评估第一个参数,然后根据第一个参数的结果评估 TRUE 或 FALSE 参数

标签: arrays excel vba dictionary count


【解决方案1】:

我想通了。代码如下:

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

    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:I" & 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
        dBT(k) = dBT(k) + IIf(d(r, COL_AOI) = "AOI Entry", 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("U7").Resize(UBound(resBT, 1), 1) = resBT


End Sub

我基本上复制了前面的代码,为相关列添加了另一个常量,并更改了对列的相关引用,并确保在计数任务之间清除字典。

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 2017-05-04
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2023-02-05
    相关资源
    最近更新 更多