Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2848|回复: 9

人员信息按照年龄范围与工龄范围汇总

[复制链接]
发表于 2015-11-28 22:45 | 显示全部楼层 |阅读模式
本帖最后由 158807837 于 2015-11-29 14:22 编辑
  1. Sub 新建工作表()
  2. On Error Resume Next

  3.     tmp = InputBox("请在文本框中输入新建工作表名称:" & Chr(13) & Chr(13) & "按【确定】增加,否则请按【取消】。", "〖点解点解〗")
  4.     If tmp = "" Then tmp = Sheet3.Name
  5.     For Each n In Sheets
  6. If tmp = n.Name Then
  7. tmp = n.Name: k = 1
  8. Exit For
  9. End If
  10. Next
  11. If k <> 1 Then
  12. Sheets.Add After:=Sheets(Sheets.Count)
  13. ActiveSheet.Name = tmp
  14. End If

  15. With Sheets(tmp)
  16. .Cells.Clear
  17. .Cells.RowHeight = Cells(2, 5)
  18. .Cells.ColumnWidth = Cells(2, 6)
  19. .Cells.HorizontalAlignment = xlCenter
  20. .Cells.VerticalAlignment = xlCenter
  21. End With
  22. Dim c As Range
  23. Dim arr
  24. arr = Sheet2.UsedRange
  25. Dim d(1 To 6) As Object
  26. Set dic = CreateObject("Scripting.Dictionary")
  27. Set d(1) = CreateObject("Scripting.Dictionary")
  28. Set d(2) = CreateObject("Scripting.Dictionary")
  29. Set d(3) = CreateObject("Scripting.Dictionary")
  30. Set d(4) = CreateObject("Scripting.Dictionary")
  31. Set d(5) = CreateObject("Scripting.Dictionary")
  32. Set d(6) = CreateObject("Scripting.Dictionary")
  33. For i = 2 To UBound(arr)
  34. If arr(i, 17) = "中专" Or arr(i, 17) = "高中" Then
  35. arr(i, 17) = "高中(含中专)"
  36. ElseIf IsNumeric(Application.Match(arr(i, 17), Split(Cells(4, 4), ","), 0)) Then
  37. arr(i, 17) = arr(i, 17)
  38. Else: arr(i, 17) = "高中以下"
  39. End If
  40. arr(i, 22) = Val(Split(arr(i, 22), "年")) * 12 + Val(Split(Split(arr(i, 22), "年")(1), "月"))

  41. dic(arr(i, 4)) = dic(arr(i, 4)) + 1
  42. d(1)(arr(i, 2) & arr(i, 4)) = d(1)(arr(i, 2) & arr(i, 4)) + 1 '年龄
  43. d(2)(arr(i, 22) & arr(i, 4)) = d(2)(arr(i, 22) & arr(i, 4)) + 1 '司龄
  44. d(3)(arr(i, 17) & arr(i, 4)) = d(3)(arr(i, 17) & arr(i, 4)) + 1 '学历
  45. d(4)(arr(i, 12) & arr(i, 4)) = d(4)(arr(i, 12) & arr(i, 4)) + 1  '岗位
  46. d(5)(arr(i, 6) & arr(i, 4)) = d(5)(arr(i, 6) & arr(i, 4)) + 1   '职等
  47. d(6)(arr(i, 14) & arr(i, 4)) = d(6)(arr(i, 14) & arr(i, 4)) + 1 '性别
  48. Next
  49. m = 5
  50. For i = 2 To 7
  51. aj = Split(Cells(i, 3), ",")
  52. bj = Split(Cells(i, 4), ",")
  53. ReDim sj(UBound(aj))
  54. For j = 0 To UBound(aj)
  55. sj(j) = Split(aj(j), "-")
  56. Next
  57. With Sheets(tmp)
  58. .Cells(m - 3, 2) = Cells(i, 1)
  59. .Cells(m - 3, 2).Font.Bold = True
  60. .Cells(m - 2, 1) = Split(Cells(1, 3), "-")(0)
  61. .Cells(m - 2, 2) = Split(Cells(1, 3), "-")(1)
  62. .Range(.Cells(m - 2, 1), .Cells(m - 1, 1)).Merge
  63. .Range(.Cells(m - 2, 2), .Cells(m - 1, 2)).Merge
  64. With .Cells(m, 1).Resize(UBound(sj) + 1, 2)
  65. .Value = Application.Transpose(Application.Transpose(sj))
  66. End With
  67. With .Cells(m, 2).Resize(UBound(sj) + 1, 1)
  68. .Font.Bold = True
  69. .Interior.ColorIndex = Cells(2, 7)
  70. End With
  71. For j = 0 To UBound(bj)
  72. .Cells(m - 2, j * 2 + 3) = bj(j)
  73. .Range(.Cells(m - 2, j * 2 + 3), .Cells(m - 2, j * 2 + 4)).Merge
  74. .Cells(m - 1, j * 2 + 3) = "人数"
  75. .Cells(m - 1, j * 2 + 4) = "占比"
  76. Next
  77. .Cells(m - 2, 3 + (UBound(bj) + 1) * 2) = "合计"
  78. .Range(.Cells(m - 2, 3 + (UBound(bj) + 1) * 2), .Cells(m - 1, 3 + (UBound(bj) + 1) * 2)).Merge
  79. .Cells(m - 2, 4 + (UBound(bj) + 1) * 2) = "占比"
  80. .Range(.Cells(m - 2, 4 + (UBound(bj) + 1) * 2), .Cells(m - 1, 4 + (UBound(bj) + 1) * 2)).Merge
  81. .Cells(m + UBound(sj) + 1, 1) = "合计"
  82. .Range(.Cells(m + UBound(sj) + 1, 1), .Cells(m + UBound(sj) + 1, 2)).Merge
  83. Set c = .Range(.Cells(m - 2, 1), .Cells(m + UBound(sj) + 1, 4 + (UBound(bj) + 1) * 2))
  84. c.Borders.LineStyle = xlContinuous
  85. c.BorderAround xlContinuous, xlMedium
  86. .Range(.Cells(m - 2, 1), .Cells(m - 1, 4 + (UBound(bj) + 1) * 2)).Interior.ColorIndex = Cells(2, 8)
  87. crr = .Range(.Cells(m - 2, 1), .Cells(m + UBound(sj) + 1, 4 + (UBound(bj) + 1) * 2))
  88. For x = 3 To UBound(crr) - 1
  89. For y = 3 To UBound(crr, 2) - 3 Step 2
  90. crr(x, y) = d(i - 1)(crr(1, y) & crr(x, 2))
  91. crr(x, y + 1) = Format(crr(x, y) / dic(crr(x, 2)), "0.00%")
  92. crr(x, UBound(crr, 2) - 1) = crr(x, UBound(crr, 2) - 1) + crr(x, y)
  93. crr(UBound(crr), y) = crr(UBound(crr), y) + crr(x, y)
  94. crr(UBound(crr), UBound(crr, 2) - 1) = crr(UBound(crr), UBound(crr, 2) - 1) + crr(x, y)
  95. Next
  96. Next
  97. For x = 3 To UBound(crr)
  98. crr(x, UBound(crr, 2)) = Format(crr(x, UBound(crr, 2) - 1) / Val(crr(UBound(crr), UBound(crr, 2) - 1)), "0.00%")
  99. Next
  100. .Cells(m - 2, 1).Resize(UBound(crr), UBound(crr, 2)) = crr
  101. End With
  102. m = m + UBound(aj) + 6
  103. Next
  104. Cells(10, 3) = arr(6, 22)
  105. Cells(11, 3) = Val(Split(arr(6, 22), "年").Value)
  106. Cells(12, 3) = Val(Split(Split(arr(12, 22), "年")(1), "月"))
  107. End Sub
复制代码

人资部报表自动化(未完成).rar

186.03 KB, 下载次数: 13

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2015-11-28 22:48 | 显示全部楼层
如表中,年龄与工龄按照参数列的条件进行统计
d(1)(arr(i, 2) & arr(i, 4)) = d(1)(arr(i, 2) & arr(i, 4)) + 1 '年龄
d(2)(arr(i, 22) & arr(i, 4)) = d(2)(arr(i, 22) & arr(i, 4)) + 1 '司龄
这报表自己做到这两天,没有想出好的方法.想是取每个条件参数的后面最大的数字,循环减前面的单元格,那就固定死了,有更改就能以调整
回复

使用道具 举报

发表于 2015-11-28 23:31 | 显示全部楼层
158807837 发表于 2015-11-28 22:48
如表中,年龄与工龄按照参数列的条件进行统计
d(1)(arr(i, 2) & arr(i, 4)) = d(1)(arr(i, 2) & arr(i, 4)) ...

为什么直接上附件呢?
回复

使用道具 举报

发表于 2015-11-29 11:37 | 显示全部楼层
这样检查,比重写还麻烦。建议:
1)上传excel文件,说明题意。
2)只需上传一部分的数据,不便公开的数据可替换一下。
回复

使用道具 举报

 楼主| 发表于 2015-11-29 14:22 | 显示全部楼层
忘记了.
回复

使用道具 举报

 楼主| 发表于 2015-11-29 16:03 | 显示全部楼层
Val(Split(arr(6, 22), "年").Value) 怎么也没办法把年 月的数字提取出来成数字*12 全部替换成月
回复

使用道具 举报

发表于 2015-11-30 15:13 | 显示全部楼层
读入数据源时以部门+年龄为key,每个分段分最小年龄,最大年龄,从for 年龄=最小年龄 To 最大年龄,提取d(key)就行了。
回复

使用道具 举报

发表于 2015-11-30 15:15 | 显示全部楼层
x=a年b月,总月=val(split(x,"年")(0))*12 +val(split(x,"年")(1))=12*a+b
回复

使用道具 举报

 楼主| 发表于 2015-11-30 21:25 | 显示全部楼层
grf1973 发表于 2015-11-30 15:15
x=a年b月,总月=val(split(x,"年")(0))*12 +val(split(x,"年")(1))=12*a+b

我已经做好了表格
代码超冗长,看有没得优化下

人资部报表自动化(完成).rar

164.24 KB, 下载次数: 21

回复

使用道具 举报

发表于 2015-11-30 21:31 | 显示全部楼层
另一个帖子不是已经回复了吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-7 02:18 , Processed in 0.299522 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表