teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2020/02/03 05:17

投稿

mattuwan
mattuwan

スコア2167

answer CHANGED
@@ -35,4 +35,41 @@
35
35
  それを使ってみました。
36
36
  エクセル的にはもっと簡単に書けると思うけど。。。。
37
37
  興味があればいってくれれば、サンプル書きます。
38
- 参考になれば。
38
+ 参考になれば。
39
+
40
+ ---
41
+
42
+ 小計機能でキーブレークの箇所に小計行を挿入し、
43
+ ジャンプ機能で分かれたデータを特定し、
44
+ 集まり毎に転記するサンプル
45
+
46
+ ```ExcelVBA
47
+ Sub test()
48
+ Dim Rng As Range
49
+ Dim a As Range
50
+ Dim i As Long
51
+
52
+ 'キーブレーク箇所に小計行挿入
53
+ With Worksheets(1).Cells(1)
54
+ .Subtotal GroupBy:=1, Function:=xlCount, TotalList:=2
55
+ With .CurrentRegion
56
+ Set Rng = Intersect(.Columns(2), .Offset(1))
57
+ End With
58
+ End With
59
+
60
+ 'キー毎(小計行は数式が入っているので定数のセルだけに特定)にコピペ
61
+ For Each a In Rng.SpecialCells(xlCellTypeConstants).Areas
62
+ i = i + 1
63
+ With Worksheets(2).Cells(1)
64
+ a.Copy .Cells(2, i)
65
+ .Cells(1, i).Value = a.Cells(1, 0).Value
66
+ End With
67
+ Next
68
+
69
+ '小計行削除
70
+ Rng.CurrentRegion.RemoveSubtotal
71
+ End Sub
72
+ ```
73
+ 必要ならば並び替えも行ってください。
74
+ 列方向の並び替えもエクセルでできる(はず。)。
75
+ 個人的にはこっちの方が、考えるのが気楽かな。。。