VBA Array with Non-Consecutive Indices

lyanik

Board Regular
Joined
Mar 11, 2014
Messages
67
I know in Python you can assign a value to an array index that doesn't exist yet. That's what I want to mimic in VBA if possible.

For example, I have a list of accounts and items they map to:

40100 => 1
42000 => 2
43000 => 8
44456 => 27

What i want is an array that I can get values from like this: arr(40100) = 1, arr(43000) = 8.

In my current workaround, I have to dimension the array to be lbound() = 40100 and ubound() = 44456 and only (4) items have values. That works, but it seems like a waste of memory and is more difficult to scroll through in the locals window.

For reference, in Python currently, the code is:

str1 = str(acct[0][0:5])
account_map[str1] = acct_holder[:]
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You are welcome. Thanks for the feedback.

Yes, the Dictionary object is very useful - using it you can make flexible and very fast codes. Worth every minute of your time learning when and how to use it.

By the way, you can also add objects (worksheets, ranges and even another dictionary) as items of a dictionary and use the methods of such objects - just a simple example of using a Dictionary of dictionaries.
http://www.techbookreport.com/tutorials/vba_dictionary2.html

But this is another story!

M.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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