ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 공급업체별 제품명 출력하기
    VB(A) 2019. 3. 17. 22:27


    왼쪽에 공급업체와 업체별 제품이름이 하나씩 나열되어있고

    버튼을 누르면

    중복된 공급업체명을 하나만 나타내고 업체별 제품명을 출력하는 매크로입니다.

    Dictionary를 사용해 중복된 공급업체를 없애고 업체별 제품명을 기록합니다.



    ConcatIf.xlsm



    Option Explicit
     
    Sub ConcatIf()
     
        Dim dict As Object
        Dim v, i As Long
     
        Application.ScreenUpdating = False
        Set dict = CreateObject("Scripting.Dictionary"'Dictionary를 선언합니다.
        v = Range("A1").CurrentRegion.Value '표에 있는 데이터를 배열에 삽입합니다.
        
        '공급업체 및 업체별 제품이름을 Dictionary에 넣습니다.
        '공급업체는 유일값으로서 Keys에, 공급업체별 제품이름은 Items에 넣습니다.
        With dict
            For i = 1 To UBound(v, 1)
     
                If v(i, 1<> vbNullString Then
                    If .exists(v(i, 1)) Then
                        .Item(v(i, 1)) = .Item(v(i, 1)) & ", " & v(i, 2)
                        
                    Else
                        .Add Key:=v(i, 1), Item:=CStr(v(i, 2))
                    End If
                End If
     
            Next i
            
            'E~F열에 공급업체 및 공급업체별 제품을 출력합니다.
            If .Count Then
     
                With Range("E1").Resize(.Count, 2)
                    .EntireColumn.ClearContents
                    .Value2 = Application.Transpose(Array(dict.keys, dict.items))
                    .EntireColumn.AutoFit
                End With
     
            End If
     
        End With
     
        Set dict = Nothing
        Application.ScreenUpdating = True
        
    End Sub
     
    cs




    댓글