vba - collection/dictionary unique value with complete range

Mallesh23

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

I need your help in extracting Unique Trade ID and paste in range F2.
But also along with that I need complete data range infrrnt of it.
Looking your help in dictionary/Collection attached expected result.

through below I can extract single column.
VBA Code:
Sub Unique()
    Dim lr As Long
    lr = Range("a1").CurrentRegion.Rows.Count
    Dim str As String
    Dim i As Integer
   
        Dim dict As New Scripting.Dictionary
   
        For i = 2 To lr
            str = Cells(i, 1).Value
          
           If Not dict.Exists(str) Then
            dict.Add (str), 1
          
           End If
   
        Next i
               
        Range("f2").Resize(dict.Count).Value = WorksheetFunction.Transpose(dict.Keys)
       
End Sub
 

Attachments

  • unique value.png
    unique value.png
    5.3 KB · Views: 17

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
What exactly do you want to return for each unique TradeID?

In the image you posted all you appear to be returning is the data from the 2 columns to the right of where you find the first instance of each unique TradeID.
 
Upvote 0
I also suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version.
 
Upvote 0
Hi Norie,

I want unique trade ID, once found, I want to add below range to collection as these are unique
Range("A2").Resize(, 3).copy
Range("A3").Resize(, 3).copy
Range("A5").Resize(, 3).copy

and want to paste collections in range(f2")

Thanks for your help in advance!



Regards,
mg
 
Upvote 0
You could change this,
VBA Code:
dict.Add (str), 1
to this

VBA Code:
dict Add str, Cells(i, 2).Resize.Value
Then change the code to populate the results to something like this.
VBA Code:
For Each ky In dict.Keys
    Range("F" & Rows.Count).End(xlUp).Value = ky
    Range("G" & Rows.Count).Resize(,2).End(xlUp).Value = dict(ky)
Next ky
 
Upvote 0
Hi Norie,

Thank you so much, it worked as expected.?

Regards,
mg
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

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