複数行にまたがっているデーターを一つの行に 2
前の質問を元にVBAを改造をしています。
(前の質問のURL:http://okwave.jp/qa/q8189711.html)
改造したものが以下です。
Sub sample()
Dim OWS As Worksheet, NWS As Worksheet
Dim myKey As String, myRow As Long, TRow As Long
Dim i As Long, j As Long
Application.DisplayAlerts = False
For Each NWS In Worksheets
If NWS.Name = "結果" Then NWS.Delete
Next
Set OWS = Sheets("Sheet1")
Set NWS = Worksheets.Add
NWS.Name = "結果"
For i = 1 To OWS.Cells(Rows.Count, 1).End(xlUp).Row
myKey = OWS.Cells(i, 1) & OWS.Cells(i, 2)
For j = 5 To OWS.Cells(i, Columns.Count).End(xlToLeft).Column
myKey = myKey & OWS.Cells(i, j)
Next j
myRow = WorksheetFunction.CountA(NWS.Columns("A:A")) + 1
If NWS.Columns("E:E").Find(What:=myKey, LookAt:=xlWhole) Is Nothing Then
NWS.Cells(myRow, 1) = OWS.Cells(i, 1)
NWS.Cells(myRow, 2) = OWS.Cells(i, 2)
NWS.Cells(myRow, 3) = OWS.Cells(i, 3)
NWS.Cells(myRow, 4) = OWS.Cells(i, 4)
NWS.Cells(myRow, 5) = myKey
Else
TRow = NWS.Columns("E:E").Find(What:=myKey, LookAt:=xlWhole).Row
NWS.Cells(TRow, 3) = NWS.Cells(TRow, 3) & "," & OWS.Cells(i, 3)
NWS.Cells(TRow, 4) = NWS.Cells(TRow, 4) & "," & OWS.Cells(i, 4)
End If
Next i
Call 同一項目削除
NWS.Columns("E:E").Delete
Application.DisplayAlerts = True
End Sub
Sub 同一項目削除()
Dim a, myDic, x
Dim h As Range
Set myDic = CreateObject("Scripting.Dictionary")
On Error Resume Next
' Range("A:A").ClearContents
For Each h In Range("E1:E" & Range("E65536").End(xlUp).Row)
a = Split(Replace(h, " ", " "), ",")
For Each x In a
myDic.Add x, ","
Next
h.Offset(0, 0) = Join(myDic.keys, ",")
myDic.RemoveAll
Next
End Sub
これをコンパクトにできますでしょうか?
お礼
ありがとうございました。