回答編集履歴

2

追記

2021/02/20 06:20

投稿

jinoji
jinoji

スコア4592

test CHANGED
@@ -123,3 +123,93 @@
123
123
  End Sub
124
124
 
125
125
  ```
126
+
127
+ ---
128
+
129
+ <再追記>
130
+
131
+ 他の方のコードを拝見し、更に修正を加えました。
132
+
133
+
134
+
135
+ ```VBA
136
+
137
+
138
+
139
+ Sub sample3()
140
+
141
+
142
+
143
+ Dim d1, d2
144
+
145
+ Set d1 = CreateObject("Scripting.Dictionary")
146
+
147
+ Set d2 = CreateObject("Scripting.Dictionary")
148
+
149
+
150
+
151
+ Dim ws, maxrow, maxcol, arr
152
+
153
+ Set ws = Sheets(1)
154
+
155
+ With ws.UsedRange
156
+
157
+ maxrow = .Rows.Count
158
+
159
+ maxcol = .Columns.Count
160
+
161
+ arr = .Value
162
+
163
+ End With
164
+
165
+
166
+
167
+ Dim c, k
168
+
169
+ For c = 1 To maxcol
170
+
171
+ k = arr(1, c)
172
+
173
+ If Not d1.Exists(k) Then Set d1(k) = CreateObject("Scripting.Dictionary")
174
+
175
+ d1(k).Add d1(k).Count, c
176
+
177
+ Next
178
+
179
+ ReDim arr2(1 To maxrow, 1 To d1.Count)
180
+
181
+
182
+
183
+ Dim r
184
+
185
+ For r = 1 To maxrow
186
+
187
+ For Each k In d1
188
+
189
+ d2.RemoveAll
190
+
191
+ For Each c In d1(k).Items
192
+
193
+ If arr(r, c) <> "" Then d2(arr(r, c)) = 0
194
+
195
+ Next
196
+
197
+ arr2(r, k) = Join(d2.keys, ":")
198
+
199
+ Next
200
+
201
+ Next
202
+
203
+
204
+
205
+ Set ws = Sheets(2)
206
+
207
+ ws.Cells.ClearContents
208
+
209
+ ws.Cells.Resize(maxrow, d1.Count).Value = arr2
210
+
211
+
212
+
213
+ End Sub
214
+
215
+ ```

1

追記

2021/02/20 06:20

投稿

jinoji
jinoji

スコア4592

test CHANGED
@@ -57,3 +57,69 @@
57
57
 
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+ ---
64
+
65
+ <追記>
66
+
67
+ 空白セルもあるとのことなので、読み飛ばす処理を追加しました。
68
+
69
+ あわせて更なる速度向上のためセル範囲を配列化しました。
70
+
71
+
72
+
73
+ ```VBA
74
+
75
+ Sub sample2()
76
+
77
+
78
+
79
+ Dim d As Object
80
+
81
+ Set d = CreateObject("Scripting.Dictionary")
82
+
83
+
84
+
85
+ Dim ws As Worksheet, arr As Variant
86
+
87
+ Set ws = Sheets("Sheet1")
88
+
89
+ arr = ws.UsedRange.Value
90
+
91
+
92
+
93
+ Dim r As Long, c As Long, k As Variant
94
+
95
+ For r = 1 To UBound(arr, 1)
96
+
97
+ For c = 1 To UBound(arr, 2)
98
+
99
+ k = Join(Array(r, arr(1, c)))
100
+
101
+ If Not d.Exists(k) Then d.Add k, CreateObject("Scripting.Dictionary")
102
+
103
+ If arr(r, c) <> "" Then d(k)(arr(r, c)) = 0
104
+
105
+ Next c, r
106
+
107
+
108
+
109
+ ws.Cells.ClearContents
110
+
111
+ For Each k In d
112
+
113
+ r = Split(k)(0)
114
+
115
+ c = Split(k)(1)
116
+
117
+ ws.Cells(r, c).Value = Join(d(k).Keys, ":")
118
+
119
+ Next
120
+
121
+
122
+
123
+ End Sub
124
+
125
+ ```