字典 求和并把其他列数据也移过去,谢谢
excel吧
全部回复
仅看楼主
level 8
团一团 楼主
2023年03月17日 02点03分 1
level 14
这个透视一下,再复制出来调整列位置不就可以么
2023年03月17日 02点03分 2
他应该是在学字典 想要看看vba要怎么写[你懂的]
2023年03月17日 03点03分
谢谢,实际上列数比较多,不能一步到位
2023年03月17日 03点03分
@阿良 是的,关键是怎么把其他的也一并获取过去
2023年03月17日 03点03分
a&b作为关键字 把c d该加的加 和 e f 用array串起来给到items。
2023年03月17日 03点03分
level 9
可以。
Sub zd()
[H2:M65535].ClearContents
n = [a65535].End(xlUp).Row
Cells(2, "H") = Cells(2, "A")
Cells(2, "I") = Cells(2, "B")
Cells(2, "L") = Cells(2, "E")
Cells(2, "M") = Cells(2, "F")
k = 2
For i = 3 To n
found = 0
For j = 2 To k
If Cells(i, "A") = Cells(j, "H") And Cells(i, "B") = Cells(j, "I") Then
found = 1
Exit For
End If
Next j
If found = 0 Then
k = k + 1
Cells(k, "H") = Cells(i, "A")
Cells(k, "I") = Cells(i, "B")
Cells(k, "L") = Cells(i, "E")
Cells(k, "M") = Cells(i, "F")
End If
Next i
For i = 2 To n
found = 0
For j = 2 To k
If Cells(i, "A") = Cells(j, "H") And Cells(i, "B") = Cells(j, "I") Then
Cells(j, "J") = Cells(j, "J").Value + Cells(i, "C").Value
Cells(j, "K") = Cells(j, "K").Value + Cells(i, "D").Value
End If
Next j
Next i
End Sub
2023年03月17日 03点03分 3
你这个是纯单元格操作 ,数据量一大就很慢
2023年03月17日 03点03分
@mongozora 数据量大就全部读入内存再处理。
2023年03月17日 07点03分
level 10
有个问题,例如描述1列,如果单号物料号都一致的多行记录,凑巧在描述1列录入内容有差异,那你的描述1列想如何处理呢?
2023年03月17日 03点03分 4
level 11
Option Explicit
Sub abc()
 Dim a, i, j, d, t, m
 a = [a1].CurrentRegion.Offset(1).Value
 Set d = CreateObject("scripting.dictionary")
 For i = 1 To UBound(a) - 1
  t = a(i, 1) & a(i, 2)
  If d.exists(t) Then
   For j = 3 To 4
    a(d(t), j) = a(d(t), j) + a(i, j)
   Next
  Else
   m = m + 1: d(t) = m
   For j = 1 To UBound(a, 2)
    a(m, j) = a(i, j)
   Next
  End If
 Next
 [a1].Offset(1, UBound(a, 2) + 1).Resize(m, UBound(a, 2)) = a
End Sub
2023年03月17日 03点03分 5
以第一次出现的行的描述为准。
2023年03月17日 03点03分
@🌴菠萝蜜🌴 行列数都可以不限
2023年03月17日 03点03分
感谢,可以使用[真棒][真棒]
2023年03月17日 07点03分
level 13
2023年03月17日 03点03分 6
看楼上蜜哥的. transpose有局限.行列不能超过65536 字符长不能超过256
2023年03月17日 04点03分
level 6
字典多字段求和要结合使用数组才行。
2023年03月17日 03点03分 7
level 11
'如果A、B列有序而且行数挺多(至少10^5),这方法会比字典快得多,而且它的效率是线性的。而字典key当多余一定数量时效率会急速下降的。
Option Explicit
Sub abc()
 Dim a, i, j, t, m, p, n(7)
 a = Range("a2:g" & Cells(Rows.Count, "a").End(xlUp).Row + 1).Value
 For i = 1 To UBound(a) - 1
  a(i, 7) = a(i, 1) & a(i, 2)
 Next
 For i = 1 To UBound(a) - 1
  For j = 3 To 4
   n(j) = n(j) + a(i, j)
  Next
  If a(i, 7) <> a(i + 1, 7) Then
   m = m + 1
   For j = 1 To UBound(a, 2)
    a(m, j) = a(p + 1, j)
   Next
   For j = 3 To 4
    a(m, j) = n(j)
    n(j) = 0
   Next
   p = i
  End If
 Next
 [a1].Offset(1, UBound(a, 2)).Resize(m, UBound(a, 2) - 1) = a
End Sub
2023年03月17日 04点03分 8
如果A、B列无序可用快排对组合列进行排序,总的效率也会高于字典的。(数据量要大,少量数据效率差不多)
2023年03月17日 04点03分
1