2016-07-12 2 views
1

죄송합니다. VBA를 처음 사용하여 죄송합니다. 나는 관련 답변을 찾으려고 노력했지만 내가 필요한 것을 해결하지 못했습니다. 많은 열이있는 데이터 목록이 있습니다. 기본적으로 나는중복 셀을 병합하고 다른 열의 값을 결합하십시오.

Before

After

가 대단히 감사합니다 같은 시트에 열 B에서 동일한 셀을 병합하고 (X로 열 C에서) 다른 모든 고유 값을 병합 할!

답변

1

이렇게하려면 scripting.dictionary을 사용합니다.

범위와 시트 이름을 수정해야합니다. 또한 1000 행 이상의 데이터를 처리하는 경우 배열의 크기를 처리해야합니다.

Sub dave() 

Dim dicKey As String 
Dim dicValues As String 
Dim dic 
Dim data 
Dim x(1 To 1000, 1 To 24) 
Dim j As Long 
Dim count As Long 
Dim lastrow As Long 

    lastrow = Cells(Rows.count, 1).End(xlUp).Row 
    data = Range("A2:X" & lastrow) ' load data into variable 
      With CreateObject("scripting.dictionary") 
        For i = 1 To UBound(data) 
         If .Exists(data(i, 2)) = True Then 'test to see if the key exists 
          x(count, 3) = x(count, 3) & ";" & data(i, 3) 
          x(count, 5) = x(count, 5) & ";" & data(i, 5) 
          x(count, 8) = x(count, 8) & ";" & data(i, 8) 
          x(count, 9) = x(count, 9) & ";" & data(i, 9) 
         Else 
          count = count + 1 
          dicKey = data(i, 2) 'set the key 
          dicValues = data(i, 2) 'set the value for data to be stored 
          .Add dicKey, dicValues 
          For j = 1 To 24 
           x(count, j) = data(i, j) 
          Next j 
         End If 
         Next i 
       End With 

       Sheets("Sheet2").Cells(2, 1).Resize(count - 1, 9).Value = x 
End Sub 
+0

답장을 보내 주셔서 감사합니다. 죄송합니다 각각의 열을 조정 한 후 오류가 발생했습니다. 줄 "subscript out of range"에 오류가 있습니다. For j = 1 To 24 – wainseven

+0

죄송합니다. 오타를 만들었으므로 코드를 다시 복사하고 다시 시도하십시오. 'A : X'에서 열을 복사해야합니다 – KyloRen

+0

대단히 감사합니다! 그것은 아름답게 작동합니다. =) – wainseven

관련 문제