回答編集履歴

2

追加

2017/09/15 00:50

投稿

kikukiku
kikukiku

スコア514

test CHANGED
@@ -241,3 +241,117 @@
241
241
  ```
242
242
 
243
243
 
244
+
245
+ 追記:RemoveDuplicatesを使って書き換え。すべての機能実現
246
+
247
+ ---
248
+
249
+ 置換前
250
+
251
+ ||A列|B列|C列|D列|E列|F列|E列|
252
+
253
+ |:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|
254
+
255
+ |1行|A|○|○|○|○|○|○|
256
+
257
+ |2行|A|△|△|△|×|×|×|
258
+
259
+ |3行|A|○|○|○|×|×|×|
260
+
261
+ |4行|B|○|○|○|○|○|○|
262
+
263
+ |5行|C|○|○|○|○|○|○|
264
+
265
+
266
+
267
+ ```VBA
268
+
269
+ Option Explicit
270
+
271
+
272
+
273
+ Sub test2()
274
+
275
+ Dim GStart As Integer
276
+
277
+ Dim GEnd As Integer
278
+
279
+ Dim RCount As Integer
280
+
281
+ Dim GName As String
282
+
283
+
284
+
285
+ RCount = 1
286
+
287
+ Do While True
288
+
289
+ GName = ActiveSheet.Cells(RCount, 1).Value
290
+
291
+ If GName = "" Then
292
+
293
+ Exit Do
294
+
295
+ End If
296
+
297
+
298
+
299
+ GStart = RCount
300
+
301
+ GEnd = グループ終端(GStart)
302
+
303
+
304
+
305
+ ActiveSheet.Range(ActiveSheet.Cells(GStart, 2), ActiveSheet.Cells(GEnd, 4)).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlNo
306
+
307
+ ActiveSheet.Range(ActiveSheet.Cells(GStart, 5), ActiveSheet.Cells(GEnd, 7)).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlNo
308
+
309
+
310
+
311
+ RCount = GEnd + 1
312
+
313
+ Loop
314
+
315
+ End Sub
316
+
317
+
318
+
319
+ Function グループ終端(GStart As Integer) As Integer
320
+
321
+ Dim RCount As Integer
322
+
323
+ Dim GName As String
324
+
325
+
326
+
327
+ RCount = GStart
328
+
329
+ GName = ActiveSheet.Cells(RCount, 1).Value
330
+
331
+ If GName = "" Then
332
+
333
+ グループ終端 = 0
334
+
335
+ Else
336
+
337
+ Do While True
338
+
339
+ If (ActiveSheet.Cells(RCount, 1).Value = GName) And (ActiveSheet.Cells(RCount + 1, 1).Value <> GName) Then
340
+
341
+ グループ終端 = RCount
342
+
343
+ Exit Do
344
+
345
+ End If
346
+
347
+ RCount = RCount + 1
348
+
349
+ Loop
350
+
351
+ End If
352
+
353
+ End Function
354
+
355
+ ```
356
+
357
+

1

追加

2017/09/15 00:50

投稿

kikukiku
kikukiku

スコア514

test CHANGED
@@ -15,3 +15,229 @@
15
15
  |A|△|△|△|×|×|×|
16
16
 
17
17
  |A|○|○|○|△|△|△|
18
+
19
+
20
+
21
+ 追記
22
+
23
+ ---
24
+
25
+ 途中で力つきまして、空白行を詰める処置が入っていませんが
26
+
27
+ あとは頑張ってみてください。
28
+
29
+ ソースはかなりベタな感じですが、一応それ以外は動いています。
30
+
31
+
32
+
33
+ ```VBA
34
+
35
+ Option Explicit
36
+
37
+
38
+
39
+ Sub test()
40
+
41
+ Dim GStart As Integer
42
+
43
+ Dim GEnd As Integer
44
+
45
+ Dim RCount As Integer
46
+
47
+ Dim GName As String
48
+
49
+
50
+
51
+ RCount = 1
52
+
53
+ Do While True
54
+
55
+ GName = ActiveSheet.Cells(RCount, 1).Value
56
+
57
+ If GName = "" Then
58
+
59
+ Exit Do
60
+
61
+ End If
62
+
63
+
64
+
65
+ GStart = RCount
66
+
67
+ GEnd = グループ終端(GStart)
68
+
69
+
70
+
71
+ 重複削除 GStart, GEnd, 2, 4
72
+
73
+ 重複削除 GStart, GEnd, 5, 7
74
+
75
+ 空白行詰 GStart, GEnd, 2, 4
76
+
77
+ 空白行詰 GStart, GEnd, 5, 7
78
+
79
+
80
+
81
+ RCount = GEnd + 1
82
+
83
+ Loop
84
+
85
+ End Sub
86
+
87
+
88
+
89
+ Function グループ終端(GStart As Integer) As Integer
90
+
91
+ Dim RCount As Integer
92
+
93
+ Dim GName As String
94
+
95
+
96
+
97
+ RCount = GStart
98
+
99
+ GName = ActiveSheet.Cells(RCount, 1).Value
100
+
101
+ If GName = "" Then
102
+
103
+ グループ終端 = 0
104
+
105
+ Else
106
+
107
+ Do While True
108
+
109
+ If (ActiveSheet.Cells(RCount, 1).Value = GName) And (ActiveSheet.Cells(RCount + 1, 1).Value <> GName) Then
110
+
111
+ グループ終端 = RCount
112
+
113
+ Exit Do
114
+
115
+ End If
116
+
117
+ RCount = RCount + 1
118
+
119
+ Loop
120
+
121
+ End If
122
+
123
+ End Function
124
+
125
+
126
+
127
+ Sub 重複削除(GStart As Integer, GEnd As Integer, ColStart As Integer, ColEnd As Integer)
128
+
129
+ Debug.Print "==========="
130
+
131
+ Dim i As Integer
132
+
133
+ Dim iEnd As Integer
134
+
135
+ Dim j As Integer
136
+
137
+ Dim jStart As Integer
138
+
139
+ Dim col As Integer
140
+
141
+ Dim 一致 As Boolean
142
+
143
+
144
+
145
+ iEnd = GEnd - 1
146
+
147
+ If iEnd = GStart Then
148
+
149
+ Exit Sub
150
+
151
+ End If
152
+
153
+
154
+
155
+ For i = GStart To iEnd
156
+
157
+ jStart = GStart + 1
158
+
159
+ If jStart > GEnd Then
160
+
161
+ Exit For
162
+
163
+ End If
164
+
165
+
166
+
167
+ 一致 = True
168
+
169
+ For col = ColStart To ColEnd
170
+
171
+ If ActiveSheet.Cells(i, col).Value = "" Then
172
+
173
+ Else
174
+
175
+ 一致 = False
176
+
177
+ Exit For
178
+
179
+ End If
180
+
181
+ Next col
182
+
183
+ If 一致 Then
184
+
185
+ GoTo tobashi
186
+
187
+ End If
188
+
189
+
190
+
191
+ For j = jStart To GEnd
192
+
193
+ If i = j Then
194
+
195
+ Else
196
+
197
+ Debug.Print i, j
198
+
199
+ 一致 = True
200
+
201
+ For col = ColStart To ColEnd
202
+
203
+ If ActiveSheet.Cells(i, col).Value = ActiveSheet.Cells(j, col).Value Then
204
+
205
+ Else
206
+
207
+ 一致 = False
208
+
209
+ Exit For
210
+
211
+ End If
212
+
213
+ Next col
214
+
215
+ If 一致 Then
216
+
217
+ For col = ColStart To ColEnd
218
+
219
+ ActiveSheet.Cells(j, col).Value = ""
220
+
221
+ Next col
222
+
223
+ End If
224
+
225
+ End If
226
+
227
+ Next j
228
+
229
+ tobashi:
230
+
231
+ Next i
232
+
233
+ End Sub
234
+
235
+
236
+
237
+ Sub 空白行詰(GStart As Integer, GEnd As Integer, ColStart As Integer, ColEnd As Integer)
238
+
239
+ End Sub
240
+
241
+ ```
242
+
243
+