|
本帖最后由 sunny568914 于 2024-3-12 14:56 编辑
- Sub 提取姓名种类唯一值到订单金额2()
- Dim arr, d As Object, i As Long, brr(), keys()
-
- Sheets("产量表").Activate
- arr = Sheets("产量表").Range("c2:D" & Cells(Rows.Count, 1).End(xlUp).Row) '将数据放进数组arr
- ReDim brr(1 To UBound(arr), 1 To UBound(arr, 2)) '定义数组brr大小
- Set d = CreateObject("scripting.dictionary")
-
- For i = 1 To UBound(arr)
- s = arr(i, 1) & arr(i, 2) '如果是3列的要求就继续使用 & 链接....
- If Not d.Exists(s) Then '如果字典不存在s这个key
- k = k + 1 '计数
- d(s) = k '不存在s这个key就让它存在
- For j = 1 To UBound(arr, 2) '将数组arr的值通过遍历放到数组brr
- brr(k, j) = arr(i, j)
- Next
- End If
- Next
-
- ' 将字典的key存入keys数组
- ReDim keys(1 To d.Count)
- i = 1
- For Each Key In d.keys
- keys(i) = Key
- i = i + 1
- Next
-
- ' 对keys数组进行排序
- Call QuickSort(keys, LBound(keys), UBound(keys))
-
- ' 按照排序后的key输出数据到"订单金额"表
- Sheets("订单金额").Range("A3").Resize(d.Count, UBound(brr, 2)) = Empty ' 清空"订单金额"表的内容
- For i = 1 To d.Count
- For j = 1 To UBound(brr, 2)
- Sheets("订单金额").Cells(i + 2, j).Value = brr(d(keys(i)), j)
- Next j
- Next i
-
- Sheets("订单金额").Activate
- End Sub
- ' 快速排序算法
- Sub QuickSort(keys As Variant, L As Long, R As Long)
- Dim i As Long, j As Long
- Dim pivot As Variant, temp As Variant
- Dim production As Variant
-
- ' 保存产量表中A2:A30的内容到production数组
- production = Sheets("总金额").Range("A2:A30").Value
-
- i = L
- j = R
- pivot = production((L + R) \ 2, 1)
-
- Do While i <= j
- Do While Sheets("订单金额").Cells(i, 1).Value < pivot
- i = i + 1
- Loop
- Do While Sheets("订单金额").Cells(j, 1).Value > pivot
- j = j - 1
- Loop
-
- If i <= j Then
- temp = Sheets("订单金额").Cells(i, 1).Value
- Sheets("订单金额").Cells(i, 1).Value = Sheets("订单金额").Cells(j, 1).Value
- Sheets("订单金额").Cells(j, 1).Value = temp
- i = i + 1
- j = j - 1
- End If
- Loop
-
- If L < j Then QuickSort keys, L, j
- If i < R Then QuickSort keys, i, R
- End Sub
复制代码 排序代码是AI生成的,但是运行会显示错误,If L < j Then QuickSort keys, L, j 溢出堆债空间
|
|