|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 吴中泉 于 2023-4-10 11:13 编辑
综合以上各位老师的代码,并适当改进,取长补短,基本符合你的要求.
(后经测试,代码有问题,数据会出错,若使用以后面楼层发的代码为准)
- Sub MergeAndSort() '合并列并根据分值排序
- With Sheet1
- ar = .[a1].CurrentRegion.Resize(, 3)
- ReDim arr(1 To UBound(ar) * 2 - 1, 1 To 2)
- For j = 1 To 2
- For i = 2 To UBound(ar)
- n = n + 1
- arr(n, 1) = ar(i, j)
- arr(n, 2) = ar(i, 3)
- Next i
- Next j
- .Range("e:f").ClearContents
- .[e1:f1] = Array("姓名", "分数")
- .[E2].Resize(UBound(arr), 2) = arr
- .[E2].Resize(UBound(arr), 2).Sort key1:=.[f2], order1:=2, Header:=xlNo
- End With
- Call 根据分数段拆分多表
- End Sub
- Sub 根据分数段拆分多表()
- Dim i, j, n, 关键字, 关键字1 As Integer
- Dim arr, dic As Object
- Set dic = CreateObject("scripting.dictionary")
- Worksheets(1).Activate
- Application.DisplayAlerts = False
- For Each wks In Worksheets
- If wks.Name <> ActiveSheet.Name Then wks.Delete
- Next
- Application.DisplayAlerts = True
- arr = [a1].CurrentRegion
- For i = 2 To UBound(arr)
- 关键字 = (arr(i, 3) \ 10) * 10
- If Not dic.exists(关键字) Then
- Set dic(关键字) = CreateObject("scripting.dictionary")
- End If
- 关键字1 = arr(i, 3)
- If Not dic(关键字).exists(关键字1) Then
- dic(关键字)(关键字1) = arr(i, 1) & "," & arr(i, 2)
- Else
- dic(关键字)(关键字1) = dic(关键字)(关键字1) & "," & arr(i, 1) & "," & arr(i, 2)
- End If
- Next
- For Each 键名 In dic.keys
- Sheets.Add(after:=Sheets(Sheets.Count)).Name = 键名
- [a1] = "姓名"
- [b1] = "分数"
- n = 2
- For Each 键名1 In dic(键名).keys
- zjsz = Application.Transpose(Split(dic(键名)(键名1), ","))
- Cells(n, "A").Resize(UBound(zjsz), 1) = zjsz
- Cells(n, "B").Resize(UBound(zjsz), 1) = 键名1
- n = n + 2
- Next
- Next
- Worksheets(1).Activate
- Beep
- End Sub
复制代码
合并列和拷贝数据到不同工作表1.zip
(23.69 KB, 下载次数: 8)
|
评分
-
1
查看全部评分
-
|