...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...
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
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)
Excel Workbook | ||||
---|---|---|---|---|
A | B | |||
1 | Banana | Yellow | ||
2 | Pear | Green | ||
3 | Orange | Orange | ||
4 | Apple | Red | ||
5 | Banana | Yellow | ||
6 | Cherry | Maroon | ||
7 | Pear | Green | ||
8 | Potato | Brown | ||
9 | Cantaloupe | Tan | ||
10 | Pear | Green | ||
Sheet1 |
Excel Workbook | ||||
---|---|---|---|---|
A | B | |||
1 | Banana | 5 | ||
2 | Pear | 10 | ||
3 | Banana | 3 | ||
Sheet1 |
Excel Workbook | ||||
---|---|---|---|---|
D | E | |||
1 | Banana | Yellow | ||
2 | Pear | Green | ||
3 | Orange | Orange | ||
4 | Apple | Red | ||
5 | Cherry | Maroon | ||
6 | Potato | Brown | ||
7 | Cantaloupe | Tan | ||
Sheet1 |
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