vba help - dictionary Items print duplicate with seperater

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am trying to print Key's and Items
Column A is (Country) a Key.
if Country comes twice as key but it has different fruits Name.

Then fruit should be combined with Seprator, as shown in Table.

Below is a Table with expected value is Column K.
VBA Code:
Sub test()

    Dim dict As New Scripting.Dictionary
  
    Dim arr_Fruit As Variant
    arr_Fruit = Range("A1").CurrentRegion.Value2

dict.RemoveAll
  
    '---------Add Data to Dictionary---------
    With dict
        .CompareMode = TextCompare
      
        For i = LBound(arr_Fruit, 1) To UBound(arr_Fruit, 1)
            If Not .Exists(arr_Fruit(i, 1)) Then
                .Add arr_Fruit(i, 1), Array(arr_Fruit(i, 2), arr_Fruit(i, 3))
            Else
                .Item(arr_Fruit(i, 1)) = .Item(arr_Fruit(i, 1))(0) & "/" & arr_Fruit(i, 2)
              
            End If
        Next i

    Dim c As Range
    For Each c In Range("f2:f4")
  
    If dict.Exists(c.Value) Then
              
        c.Offset(, 1).Value = .Item(c.Value)(0)
  
    End If
  
    Next c

    End With


End Sub





Book12
ABCDEFGHIJ
1CountryFruitColorNameFruit/colorExpected
2IndiaMangoYellowIndiaMangoNameFruit/color
3AustraliaAppleGreenAustraliaMangoIndiaMango
4EnglandBananaYellowEnglandAustraliaMango
5EnglandMangoBlueEnglandBanana/Mango
6
Sheet1


Thanks
mg
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
VBA Code:
Sub test()

    Dim dict As New Scripting.Dictionary
  
    Dim arr_Fruit As Variant, Tmp As Variant
    arr_Fruit = Range("A1").CurrentRegion.Value2

dict.RemoveAll
  
    '---------Add Data to Dictionary---------
    With dict
        .CompareMode = TextCompare
      
        For i = LBound(arr_Fruit, 1) To UBound(arr_Fruit, 1)
            If Not .Exists(arr_Fruit(i, 1)) Then
                .Add arr_Fruit(i, 1), Array(arr_Fruit(i, 2), arr_Fruit(i, 3))
            Else
                Tmp = .Item(arr_Fruit(i, 1))
                Tmp(0) = Tmp(0) & "/" & arr_Fruit(i, 2)
                .Item(arr_Fruit(i, 1)) = Tmp
            End If
        Next i

    Dim c As Range
    For Each c In Range("f2:f4")
  
    If dict.Exists(c.Value) Then
              
        c.Offset(, 1).Value = .Item(c.Value)(0)
  
    End If
  
    Next c

    End With


End Sub
 
Upvote 0
Hi Fluff,

Wow ! perfect , it worked, got as expected. millions of thanks for help. ?


Thanks
mg
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Unique Dictionary With Concatenation

VBA Code:
Option Explicit

Sub test()
    
    Const sFirst As String = "A1"
    Const sFirstRow As Long = 2
    Const tFirst As String = "F2"
    Const Delimiter As String = "/"
    
    ' Write values from Source Range to Data Array ('Data').
    Dim Data As Variant
    Data = Range(sFirst).CurrentRegion.Value

    With CreateObject("Scripting.Dictionary")
        ' Write values from Data Array to Unique Dictionary.
        .CompareMode = vbTextCompare
        Dim i As Long
        For i = sFirstRow To UBound(Data, 1)
            If Not .Exists(Data(i, 1)) Then
                .Add Data(i, 1), Data(i, 2)
            Else
                .Item(Data(i, 1)) = .Item(Data(i, 1)) & Delimiter _
                                                      & Data(i, 2)
            End If
        Next i
        ' Write keys and their values from Unique Dictionary to Target Range.
        Dim rng As Range
        Set rng = Range(tFirst).Resize(.Count)
        rng.Value = Application.Transpose(.Keys)
        rng.Offset(, 1).Value = Application.Transpose(.Items)
    End With
    
End Sub
 
Upvote 0
Hi VBbasic08,

Perfect ! Thank you much for sharing another way.

Thanks
Mg
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,124,999
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top