2017-05-23 1 views
0

엑셀 나는 Excel에서 다음과 같은 데이터가 있습니다찾는 조합이 여러 행이

Ingredient1 Ingredient2 Ingredient3 Ingredient 4 
Salami  Tuna   Peperoni  Mushroom 
Salami  Peperoni  Mushroom 
Salami  Mushroom 

성분은 열 이름이며, 충분한 재료가 없기 때문에 일부 세포가 비어 있습니다.

내가 예를 들어, 조합과 수를 좀하고 싶습니다

:

Salami, Tuna  1 
Salami, Peperoni 2 
Salami, Mushroom 3 

는 것이 가능할까요?

+0

살라미, 참치 1? 옳은? – Masoud

+0

'Salami'가 변경되면 'COUNTIF'또는 'COUNTIFS'를 사용하면됩니다. – Tom

답변

0

1 비트. 원한다면 이걸 줄 수 있어요. 특정 입력 및 출력에 대해 수정이 필요하지만 작동합니다. 첫 번째 열과 각 개별 성분 및 수를 합친 것입니다. 즉 '버섯, 페퍼로니'와 같은 조합은 반환되지 않지만 질문에 지정한 내용과 일치하지 않습니다.

Option Explicit 
Public Sub CountDemo() 
    Dim dict As Object 
    Dim rng As Range 
    Dim c 
    Dim Ing As String 
    Dim i As Long 
    Dim key 
    '' Set up for dictionary object 
    Set dict = CreateObject("Scripting.Dictionary") 
    '' Declare data range 
    With Sheet1 
     Set rng = .Range(.Cells(2, 2), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 4)) 
    End With 

    '' Loop through each c in range 
    For Each c In rng.Cells 
     '' Test if cell is null 
     If Not c.Value2 = vbNullString Then 
      '' Set Ingredient combo to variable 
      Ing = c.Offset(0, 1 - c.Column).Value2 & ", " & c.Value2 
      '' Test if value exists in dictionary 
      If dict.exists(Ing) Then 
       '' If exists increase count by 1 
       dict(Ing) = dict(Ing) + 1 
      Else 
       '' If doesn't exist then add to dictionary and set to default value of 1 
       dict.Add key:=Ing, Item:=1 
      End If 
     End If 
    Next c 

    '' Output results 
    'Starting row 
    i = 8 
    '' Loop through each ingredient combo 
    For Each key In dict.keys 
     '' Print to sheet values 
     With Sheet1.Cells(i, 1) 
      .Value2 = key 
      .Offset(0, 1).Value2 = dict(key) 
     End With 
     i = i + 1 
    Next key 
End Sub