返回列表 上一主題 發帖

使用VBA求資料統計輸出

回復 3# b9208
附件內樞紐分析表方法供你參考 H1_統計.rar (16.29 KB)
  1. Private Sub CommandButton1_Click()
  2. Set d = CreateObject("Scripting.Dictionary")
  3. Set d1 = CreateObject("Scripting.Dictionary")
  4. Set d2 = CreateObject("Scripting.Dictionary")
  5. With Sheet3
  6.    For Each a In .Range(.[B6], .[B65536].End(xlUp))
  7.       ar = Array(a, a.Offset(, 2), a.Offset(, 3), "")
  8.       d(a.Offset(, 3).Value) = d(a.Offset(, 3).Value) + 1
  9.       ar(3) = d(a.Offset(, 3).Value)
  10.       d1(a.Offset(, 3).Value) = ar
  11.       d2(a.Value) = d2(a.Value) + 1
  12.     Next
  13. End With
  14. With Sheets("統計表")
  15. .[B7:F65536] = ""
  16. .[B7].Resize(d1.Count, 4) = Application.Transpose(Application.Transpose(d1.items))
  17. .[B6].Resize(d1.Count + 1, 4).Sort key1:=.[B7], key2:=.[C7], header:=xlYes
  18. .[G7:H65536] = ""
  19. .[G7].Resize(d2.Count, 1) = Application.Transpose(d2.keys)
  20. .[H7].Resize(d2.Count, 1) = Application.Transpose(d2.items)
  21. .[G6].Resize(d2.Count + 1, 2).Sort key1:=.[G7], header:=xlYes
  22. .Select
  23. End With
  24. End Sub
複製代碼
學海無涯_不恥下問

TOP

回復 6# aa7551


    Sheet3是資料表的CodeName
未命名.png
學海無涯_不恥下問

TOP

回復 12# b9208
  1. Private Sub CommandButton1_Click()
  2. Set d = CreateObject("Scripting.Dictionary")
  3. Set d1 = CreateObject("Scripting.Dictionary")
  4. Set d2 = CreateObject("Scripting.Dictionary")
  5. With Sheet3
  6.    For Each a In .Range(.[B6], .[B65536].End(xlUp))
  7.       ar = Array(a, a.Offset(, 2), a.Offset(, 3), "")
  8.      m = Join(ar)
  9.       d(m) = d(m) + 1 '3欄統計
  10.       ar(3) = d(m)
  11.       d1(m) = ar
  12.       d2(a.Value) = d2(a.Value) + 1 '地區統計
  13.     Next
  14. End With
  15. With Sheets("統計表")
  16. .[B7:F65536] = ""
  17. .[B7].Resize(d1.Count, 4) = Application.Transpose(Application.Transpose(d1.items))
  18. .[B6].Resize(d1.Count + 1, 4).Sort key1:=.[B7], key2:=.[C7], header:=xlYes
  19. .[G7:H65536] = ""
  20. .[G7].Resize(d2.Count, 1) = Application.Transpose(d2.keys)
  21. .[H7].Resize(d2.Count, 1) = Application.Transpose(d2.items)
  22. .[G6].Resize(d2.Count + 1, 2).Sort key1:=.[G7], header:=xlYes
  23. .Select
  24. End With
  25. End Sub
複製代碼
學海無涯_不恥下問

TOP

回復 16# c_c_lai
可利用錄製巨集來取得VBA代碼
這樣可同時熟悉EXCEL的基本操作
您不妨先試試看
學海無涯_不恥下問

TOP

        靜思自在 : 世上有兩件事不能等:一、孝順 二、行善。
返回列表 上一主題