GetValues("凭证号","对方科目 is null")和Compute("sum(贷方)","凭证号 = '" & pz & "'")可以合并使用分组统计生成,下面部分貌似没有优化的可能
Dim t1 As DataTable = DataTables("Sheet3")
Dim b As New GroupTableBuilder("统计表1",t1)
b.Groups.AddDef("凭证号") '根据产品分组
b.Totals.AddDef("贷方","合计") '对数量进行统计
b.Filter = "对方科目 is null"
Dim dt As DataTable = b.Build(True) '生成统计表
For Each dr As DataRow In dt.DataRows
Dim hj As Double
hj = dr("合计")
For Each a As DataRow In t1.Select("借方 is not null And 借方 <> 0 And 凭证号 = '" & dr("凭证号") & "'","_sortkey")
a("凭证号") = pz & "拆"
For Each b As DataRow In t1.Select("贷方 is not null and 贷方 <> 0 And 凭证号 = '" & dr("凭证号") & "'","_sortkey")
Dim tr As DataRow = t1.AddNew()
tr("一级科目名称") = a("一级科目名称")
tr("借方") = a("借方") * b("贷方")/hj
tr("对方科目") = b("一级科目名称")
tr("凭证号") = pz
b("凭证号") = pz & "拆"
Next
Next
Next