2016-12-01 2 views
0

とdictのキーを定義する:は、私が辞書から列の値を印刷するには、このコードを持って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の値を持つ試験のためのキーが欲しいです

'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 

しかし、私は「上「下付き文字の範囲外」エラーが出るように私は、私は適切にセルアドレスを定義したとは思わない:列Tにだから私はここでIfステートメントを挿入しました"行。

Col TをCOL_AOIやCOL_RTのような定数として定義しようとしましたが、動作しません。他に何が問題になるのか分かりません。その列Tはあなたの配列にアクセスすることはできません

+0

変数 'd'を宣言する場所のコード部分と値を代入する部分を含めることができます。また、 'If'文の直前に' Debug.Print UBound(d、2) 'を置いて、それが何であるか教えてください。 – YowE3K

+0

そこに行くよ@ YowE3K – shecodes

+0

もちろん!それがうまくいく、ありがとう。それでもDebug.Print UBound(d、2)が何を言いたいのですか? – shecodes

答えて

0

の理由は、あなたが言って配列を作成しているということです。

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使用)

をあなたはその後、値にアクセスすることができるはずです

If d(r, 19) = 1 Then 

(使用19ではなく20、列Bは、アレイの位置r, 1になるため、カラムCは、アレイの位置r, 2内等になる)

:列Tからのような文を使用して
+0

このソリューションは初めてでしたし、結果をクリアしてもう一度やり直してみましたが、Col Uで何も印刷されません。 – shecodes

+0

@shecodes列Tに何かがある場合、その中の1以外。だから、キーの最後のエントリに列Tの1が入っている場合はありますか? – YowE3K

+0

キーの大部分はT列に1の値を持つ必要があります。そのうちのいくつかは2、おそらくそれ以上の値を持ちます。最後のエントリが何を意味するかわからない解明のためにデータファイルを見たいのですか? [ここにある](http://www.filedropper.com/fulltest_1)。 – shecodes

関連する問題