|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
也来练个手——
- <div>Sub 整理数据()
- Dim rngDB As Range, arr, arrRT()
- Dim dic As Object, i&, j&, k, v
- Set dic = CreateObject("SCRIPTING.DICTIONARY") '创建字典对象
- '给出对话框提示选择源数据,包含标题行
- Set rngDB = Application.InputBox("请选择源数据所在单元格区域:", Default:=[B2].CurrentRegion.Address, Type:=8)
- If Not rngDB Is Nothing Then
- arr = rngDB '将源数据存入数组
- For i = 2 To UBound(arr, 1) '行循环
- For j = 3 To UBound(arr, 2) '列循环
- If Len(arr(i, j)) > 0 And arr(i, 2) <> "停止" Then
- '将姓名与项目匹配后存入字典dic
- If dic.exists(arr(i, j)) Then
- dic.Item(arr(i, j)) = dic.Item(arr(i, j)) & "," & arr(i, 1)
- Else
- dic.Add arr(i, j), arr(i, 1)
- End If
- End If
- Next j
- Next i
- End If
- ReDim arrRT(dic.Count + 1, UBound(arr)) '定义结果存储数组
- arrRT(0, 0) = "项目"
- k = dic.keys '从字典中获取项目名称数组
- For i = 0 To UBound(k)
- arrRT(i + 1, 0) = k(i) '存储项目名称
- v = Split(dic.Item(k(i)), ",") '获取项目名称对应的姓名数组
- For j = 0 To UBound(v)
- arrRT(0, j + 1) = "姓名" & j + 1 '存入列标题
- arrRT(i + 1, j + 1) = v(j) '存入姓名
- Next j
- Next i
- '提示选择单元格并存入结果
- With Application.InputBox("请选择存储结果的单元格:", Default:=[T2].Address, Type:=8)
- Application.ScreenUpdating = False '关闭屏幕刷新
- .CurrentRegion.ClearContents '清空当前单元格所在区域已在的内容
- .Resize(dic.Count + 1, UBound(arr)) = arrRT '写入结果数组
- .CurrentRegion.Sort key1:=.Cells(1), Header:=xlYes '按项目名称进行排序
- Application.ScreenUpdating = True '打开屏幕刷新
- End With
- '打扫战场
- Set rngDB = Nothing
- End Sub</div>
复制代码
|
评分
-
1
查看全部评分
-
|