2016-12-01 83 views
0

我有这样的代码从字典中打印列中的值:与if语句定义字典键

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:Q" & 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 d(r, 20) = 1 Then 
     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 

在界定计数AOI条目中的关键,我只想键具有值1试验在列T.所以我插入I​​f语句这里:

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

但我不认为我正确定义单元格地址,我得到一个“下标超出范围”出错了“如果“行”。

我试着将COL T定义为像COL_AOI或COL_RT这样的常量,但它也不起作用。我不知道还有什么问题可以解决。

+0

你可以在声明变量'd'的地方包含代码的一部分,以及为其赋值的部分。也许可以在'If'语句之前立即放置一个'Debug.Print UBound(d,2)',并告诉我们它说了什么。 – YowE3K

+0

你去哪里@ YowE3K – shecodes

+0

当然!这很有用,谢谢。还想看看Debug.Print UBound(d,2)说什么? – shecodes

回答

0

的原因,列T是不是你的数组中访问是你说创建阵列:

Set rng = sht.Range("B7:Q" & lastrow) 
d = rng.Value 

这只会在列B和Q之间的值复制为了访问列T作为好,改变说法是:

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

(或者更简单地说,只是用d = sht.Range("B7:T" & lastrow).Value

然后,您应该能够访问值从塔T通过使用语句,例如:

If d(r, 19) = 1 Then 

(使用19,而不是20,因为B列将在所述阵列的r, 1位置,列C将在该阵列的r, 2位置等)

+0

所以这个解决方案工作的第一次,然后我清理的结果,并试图再次,它不打印在山口U.什么 – shecodes

+0

@shecodes您的代码表示重置计数的关键,如果塔T有什么除了1之外。那么,你是否有过任何一个关键字的最后一项在T列中包含1的情况? – YowE3K

+0

大多数键在T列中的值应为1,其中一些键将有2,可能更高。不确定你最后一次输入的含义。你想看清楚数据文件吗? [它在这里](http://www.filedropper.com/fulltest_1)。 – shecodes