Collection library

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960
Hi, if I had a range from A1 : A100 contained many duplicated items, I was told there was a method in VBA to add all of these to an array, but it would only store the unique values? A collection or library

Any ideas?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You can use a Dictionary or Collection Object.
 
Upvote 0
Those were the ones!

How would I go about using them?

My ideal situation would be that it goes through a range e.g. A1:A100 only storing unique items. But then I also need a second array containing the adjacent value in column B.

So the two arrays will be the same size, but the first will do all of the logical work and the second will be based on the index of the first?

If not can just form a unique array and get the column B details using a LOOKUP (but not efficient)

Any help apprecaited!
 
Upvote 0
...My ideal situation would be that it goes through a range e.g. A1:A100 only storing unique items. But then I also need a second array containing the adjacent value in column B...

Hi Theta,

I am not clear on that part. Presuming there are corresponding vals in Col B for ea val in Col A, and Col A has duplicates, once we rid the duplicates, how are we picking a certain val in B to go along with a particular val (unique now) from A?

Anyways, here is a short example to run in a blank/new workbook. Both in a Standard Module, run Setup to get some junk vals and Exa to retunr the uniques in Col B.
Rich (BB code):
Option Explicit
    
Sub Setup()
    With Sheet1.Range("A1:A100")
        .Formula = "=ROUND(RAND()*25,0)"
        .Value = .Value
    End With
End Sub
    
Sub exa()
Dim DIC As Object '<--- Dictionary
Dim rngMyRange As Range
Dim aryMyArray
Dim i As Long
    
    Set rngMyRange = Sheet1.Range("A1:A100")
    Set DIC = CreateObject("Scripting.Dictionary")
    
    aryMyArray = rngMyRange.Value
    
    For i = 1 To UBound(aryMyArray, 1)
        DIC.Item(Key:=aryMyArray(i, 1)) = Empty
    Next
    
    aryMyArray = DIC.Keys
    
    Sheet1.Range("B1").Resize(UBound(aryMyArray, 1) + 1).Value _
        = Application.Transpose(aryMyArray)
    
End Sub

Hope that helps,

Mark
 
Upvote 0
Hi, yes you are correct. For each item in A, I would also like to store the adjacent item in column B in it's own array

The two should be related. So if A is duplicated, it will always have the same column B value (imagine fruit being A, and colour being B...so Banana will always have Yellow in the adjacent column)

I will give this a go, it is going to save me alot of time...
 
Upvote 0
Hi Mark,

So looking at your code...if I wanted to store the adjacent value I could just form a second array and load values to it using 'i' as the index? (as both original data arrays are the same size)

I could then call items from both arrays using the same index and they should tie up

See any problem with this logic?

(I am also giving this a read : http://www.cpearson.com/excel/CollectionsAndDictionaries.htm)
 
Upvote 0
Hi, yes you are correct. For each item in A, I would also like to store the adjacent item in column B in it's own array...

Hi Mark,

So looking at your code...if I wanted to store the adjacent value I could just form a second array and load values to it using 'i' as the index? (as both original data arrays are the same size)

I could then call items from both arrays using the same index and they should tie up

See any problem with this logic?

(I am also giving this a read : http://www.cpearson.com/excel/CollectionsAndDictionaries.htm)

Okay, I am probably just being thick-headed, but just to confirm, does your data look something like:
Excel Workbook
AB
1BananaYellow
2PearGreen
3OrangeOrange
4AppleRed
5BananaYellow
6CherryMaroon
7PearGreen
8PotatoBrown
9CantaloupeTan
10PearGreen
Sheet1
Excel 2003

That is, we are not going to find different vals is Col B for the same val in Col A, like:
Excel Workbook
AB
1Banana5
2Pear10
3Banana3
Sheet1
Excel 2003
 
Upvote 0
Okay, then we could also assign/overwrite the val of .Item for ea Key.

For:
Excel Workbook
DE
1BananaYellow
2PearGreen
3OrangeOrange
4AppleRed
5CherryMaroon
6PotatoBrown
7CantaloupeTan
Sheet1
Excel 2003

Maybe:
Rich (BB code):
Sub exa()
Dim DIC As Object '<--- Dictionary
Dim rngMyRange As Range
Dim aryMyArray, TmpColA, TmpColB
Dim i As Long
    With Sheet1
        Set rngMyRange = Range(.Range("A1"), .Cells(.Rows.Count, "B").End(xlUp))
        
        Set DIC = CreateObject("Scripting.Dictionary")
        
        aryMyArray = rngMyRange.Value
        
        For i = 1 To UBound(aryMyArray, 1)
            DIC.Item(Key:=aryMyArray(i, 1)) = aryMyArray(i, 2)
        Next
        
        '// These are both zero-based 1-dimension arrays    //
        TmpColA = DIC.Keys
        TmpColB = DIC.Items
        
        ReDim aryMyArray(1 To DIC.Count, 1 To 2)
        
        '// Or, we could store both arrays from above in one 2-dimensional array//
        For i = 1 To DIC.Count
            aryMyArray(i, 1) = TmpColA(i - 1)
            aryMyArray(i, 2) = TmpColB(i - 1)
        Next
        
        .Range("D1").Resize(UBound(aryMyArray, 1), 2).Value _
            = aryMyArray
    End With
End Sub
 
Upvote 0
Fantastic! I will get to work using this method.

I will only have to adapt it to page ranges (I have allocated 40 lines per page) so that I can select elements 1-40, 41-80, 81-100 from each array

?
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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