Excel 在新工作表中打印字典键

Excel 在新工作表中打印字典键,excel,vba,dictionary,Excel,Vba,Dictionary,以下代码使用dictionary对象对按下的按钮和AOI条目进行计数,然后将计数打印到同一工作表上的其他列: 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

以下代码使用dictionary对象对按下的按钮和AOI条目进行计数,然后将计数打印到同一工作表上的其他列:

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
Dim dBT As Object'全局字典
子按钮压力计数()
'列位置的常数
Const COL_块的长度=1
Const COL_试验长度=2
Const COL_动作时长=7
常数colu_AOI,长度=8
尺寸标注为范围,最后一行为长度,第二行为工作表
Dim d,r等于Long,k,resBT()
Set sht=工作表(“全面测试”)
lastrow=单元格(Rows.Count,3).End(xlUp).Row
Set dBT=CreateObject(“scripting.dictionary”)
设置rng=sht.Range(“B7:I”和lastrow)
d=rng.Value“将数据放入数组
ReDim resBT(1到UBound(d),1到1)“调整数组大小,这将
“被放在马驹里
'获得每个模块的独特组合和试压计数
对于r=1到UBound(d,1)
k=d(r,COL_块)和“|”&d(r,COL_试用)”创建密钥
dBT(k)=dBT(k)+IIf(d(r,COL_ACT)”,1,0)
下一个r
'使用每行的适当计数填充数组
对于r=1到UBound(d,1)
k=d(r,1)&“|”和d(r,2)'创建密钥
resBT(r,1)=dBT(k)'获取计数
下一个r
'将数组放置到工作表
短范围(“T7”)。调整大小(UBound(resBT,1),1)=resBT
“清晰的字典
dBT.RemoveAll
'统计AOI条目
对于r=1到UBound(d,1)
k=d(r,COL_块)和“|”&d(r,COL_试用)”创建密钥
dBT(k)=dBT(k)+IIf(d(r,COL_AOI)=“AOI条目”,1,0)
下一个r
'使用每行的适当计数填充数组
对于r=1到UBound(d,1)
k=d(r,1)&“|”和d(r,2)'创建密钥
resBT(r,1)=dBT(k)'获取计数
下一个r
'将数组放置到工作表
短程范围(“U7”)。调整大小(UBound(resBT,1),1)=resBT
端接头
现在,我希望将代码的“计数AOI条目”位打印到使用此代码创建的新工作表中的指定单元格:

Sub createsummarytable()
'add new worksheet to data
Dim datasummary As Worksheet

With ThisWorkbook.Worksheets.Add
.Name = "datasummary"

Dim i As Long
Dim j As Long
Dim t As Long
Dim Startrow As Long

Startrow = -4
t = 1

'print Block number headings
For i = 1 To 40
    If i < 31 Then
    .Cells(Startrow + (5 * i), 1).Value = "Block " & i

    Else     'print transfer block headings

    .Cells(Startrow + (5 * i), 1).Value = "Transfer Block " & t
    t = t + 1
    End If

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

End With
End Sub
Sub-createsummarytable()
'将新工作表添加到数据
Dim数据摘要作为工作表
使用此工作簿。工作表。添加
.Name=“数据摘要”
我想我会坚持多久
Dim j尽可能长
我没那么长
黯淡的星空如长
Startrow=-4
t=1
'打印区块编号标题
对于i=1到40
如果我<31那么
.Cells(Startrow+(5*i),1.Value=“Block”&i
Else的打印传输块标题
.Cells(Startrow+(5*i),1.Value=“传输块”&t
t=t+1
如果结束
'打印试用编号标题
对于j=1到18
.Cells((Startrow+1)+(5*i),j)
下一个j
接下来我
以
端接头
我知道在新的工作表中格式化表格的代码是粗略的,理想情况下会合并到第一个代码模块中。我将在将来研究这个问题

我想让AOI条目计数进入试用下面的第一行,但不知道如何将每个试用的计数分开,并将其分为不同的行和列

'....
'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

createsummarytable

PopSummary dBT
Sub填充汇总表(可能需要检查“未找到”案例…)

Sub填充汇总表(可能需要检查“未找到”案例…)


蒂姆,真是太棒了,再次感谢你。请继续关注我的下一个任务,我需要在下一行打印RTs,并对试验进行平均;)蒂姆,真是太棒了,再次感谢你。请继续关注我的下一个任务,我需要在下一行打印RTs,并对试验进行平均;)
Sub PopSummary(dict)

    Dim sht As Worksheet, k, b, t, f, f2

    Set sht = ThisWorkbook.Sheets("datasummary")

    For Each k In dict

        b = Split(k, "|")(0) 'get block
        t = Split(k, "|")(1) 'get trial
        'find the block
        Set f = sht.Columns(1).Find(what:=b, lookat:=xlWhole, LookIn:=xlValues)
        If Not f Is Nothing Then
            'find the trial under that block
            Set f2 = f.Offset(1, 0).EntireRow.Find(what:=t, lookat:=xlWhole, LookIn:=xlValues)
            If Not f2 Is Nothing Then f2.Offset(1, 0).Value = dict(k)
        End If
    Next k

End Sub