|
楼主 |
发表于 2024-1-3 18:15
|
显示全部楼层
- Sub ss()
- Dim ar, i&, d As Object, x, xx, pj&, n&, a, b, c, an&, bn&, cn&
- Set d = CreateObject("scripting.dictionary")
- '取数
- With Sheets("数据源")
- ar = .Range("A1:D" & .Cells(.Rows.Count, 1).End(3).Row)
- End With
- ReDim a(1 To UBound(ar), 1 To 4)
- b = a
- c = a
- '数据置入字典
- For i = 2 To UBound(ar)
- x = ar(i, 1)
- If Not d.exists(x) Then Set d(x) = CreateObject("scripting.dictionary")
- d(x)(ar(i, 2)) = ar(i, 3) & "-" & ar(i, 4)
- Next
- '分配至数组
- For Each x In d.keys
- pj = Int(d(x).Count / 3) '平均数
- n = 0
- For Each xx In d(x).keys
- n = n + 1
- Select Case n
- Case Is <= pj
- an = an + 1
- a(an, 1) = x
- a(an, 2) = xx
- a(an, 3) = Split(d(x)(xx), "-")(0)
- a(an, 4) = Split(d(x)(xx), "-")(1)
- Case Is <= pj * 2
- bn = bn + 1
- b(bn, 1) = x
- b(bn, 2) = xx
- b(bn, 3) = Split(d(x)(xx), "-")(0)
- b(bn, 4) = Split(d(x)(xx), "-")(1)
-
- Case Else
- cn = cn + 1
- c(cn, 1) = x
- c(cn, 2) = xx
- c(cn, 3) = Split(d(x)(xx), "-")(0)
- c(cn, 4) = Split(d(x)(xx), "-")(1)
- End Select
- Next
- Next
- '写入分表
- Sheet2.Range("A2:d" & Rows.Count).ClearContents
- Sheet3.Range("A2:d" & Rows.Count).ClearContents
- Sheet4.Range("A2:d" & Rows.Count).ClearContents
- Sheet2.[A2].Resize(UBound(a), 4) = a
- Sheet3.[A2].Resize(UBound(b), 4) = b
- Sheet4.[A2].Resize(UBound(c), 4) = c
- MsgBox "完成"
- End Sub
复制代码 |
|