Add Range to Dictionary

30percent

Board Regular
Joined
May 5, 2011
Messages
118
Office Version
  1. 2016
Platform
  1. Windows
Hi,

how do I add a range to dictionary using VBA? As an example shown below, using Product as key and Order as value:

ProductOrder
Chair10
Table5
Pencil20
Pen30
Printer1
Phone5

<tbody>
</tbody>

Thank you!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
There are numerous examples on the forum. Here's some sample code from one such post:

Rich (BB code):
Dim MyDict As Object, i As Long, MyVals As Variant

    Set MyDict = CreateObject("Scripting.Dictionary")
    
    MyVals = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Value
    For i = 1 To UBound(MyVals)
        MyDict(MyVals(i, 1)) = MyVals(i, 2)
    Next i

I've assumed there would be no duplicate product names?
 
Upvote 0
Hi @RoryA,

thank you!
There are no duplicate keys.

Wonder if it is possible to create a dictionary without using loop.
 
Upvote 0
No it is not. Why can’t you loop?
 
Upvote 0
There are numerous examples on the forum. Here's some sample code from one such post:

Rich (BB code):
Dim MyDict As Object, i As Long, MyVals As Variant

    Set MyDict = CreateObject("Scripting.Dictionary")
   
    MyVals = Range("A2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Value
    For i = 1 To UBound(MyVals)
        MyDict(MyVals(i, 1)) = MyVals(i, 2)
    Next i

I've assumed there would be no duplicate product names?
Hi Rory,
How do you print this dictionary on a worksheet?
 
Upvote 0
@JohnExcel222 - What exactly are you wanting to do? If all you want is to store and existing range in memory, maybe do some calculations or manipulations to the data and then print it back out to a worksheet, then storing that data in a VBA array would be more efficient than using a Dictionary.
 
Upvote 0
@JohnExcel222 - What exactly are you wanting to do? If all you want is to store and existing range in memory, maybe do some calculations or manipulations to the data and then print it back out to a worksheet, then storing that data in a VBA array would be more efficient than using a Dictionary.
Hi Rick,
I would like to develop an order management / costs and invoicing management / tracker, with daily new work orders, constantly changing status, updates.... and I need to lookup into several tables to have a picture of what is going on ..... I try to get into dictionaries and arrays .... but is not easy. here is what I try to do .... (as a first step) .... Lookup in other sheets
 
Upvote 0
@JohnExcel222 - What exactly are you wanting to do? If all you want is to store and existing range in memory, maybe do some calculations or manipulations to the data and then print it back out to a worksheet, then storing that data in a VBA array would be more efficient than using a Dictionary.
ah ok, I need to focus on arrays then
 
Upvote 0
ah ok, I need to focus on arrays then
Loading an array is nearly instantaneous. Assuming a Variant variable named Arr and the range A2:M99

Arr = Range("A2:M99").Value

You can then loop through the array by iterating a For..Next counter from 1 (arrays created this way always have a lower bound of 1) to UBound(Arr), do whatever manipulations you want and write the new values back to the original array, if appropriate, or to a new Variant variable ReDim'med to the size of the Arr array (remember, Arr is a two-dimensional array). You can then write the manipulated array (I'll use Arr for this example, but it could just as easily be the "new Variant variable" I mentioned a moment ago) back to whatever range you want (here, I start at Column P for example purposes, but you could write right over the original array if that is what is needed)...

Range("P2").Resize(UBound(Arr, 1), UBound(Arr, 2)) = Arr

Writing this array back is also nearly instantaneous as well.
 
Upvote 0
Loading an array is nearly instantaneous. Assuming a Variant variable named Arr and the range A2:M99

Arr = Range("A2:M99").Value

You can then loop through the array by iterating a For..Next counter from 1 (arrays created this way always have a lower bound of 1) to UBound(Arr), do whatever manipulations you want and write the new values back to the original array, if appropriate, or to a new Variant variable ReDim'med to the size of the Arr array (remember, Arr is a two-dimensional array). You can then write the manipulated array (I'll use Arr for this example, but it could just as easily be the "new Variant variable" I mentioned a moment ago) back to whatever range you want (here, I start at Column P for example purposes, but you could write right over the original array if that is what is needed)...

Range("P2").Resize(UBound(Arr, 1), UBound(Arr, 2)) = Arr

Writing this array back is also nearly
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

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