Two dim array by Object, problem writing new value into x,y position

PeterBunde

New Member
Joined
Dec 7, 2016
Messages
45
Fellow sufferers

I am using Object class to create a two-dimensional array. See code below.

Sometimes, I ned to overwrite the valie at x,y. Now, if that value has already been set, there is an error. But just asking if it is empty, apparently sets the value, but to "".

What is the right code to write a value into the x,y position - with or without it being previously defined?

Code:
Sub Test()
Dim inhabitants_in As Object
Set inhabitants_in = CreateObject("Scripting.Dictionary")
' add countries
inhabitants_in.Add "Norway", CreateObject("Scripting.Dictionary")
inhabitants_in.Add "France", CreateObject("Scripting.Dictionary")
inhabitants_in.Add "Italy", CreateObject("Scripting.Dictionary")
' add cities and number of inhabitants
inhabitants_in("Norway").Add "Oslo", 9
inhabitants_in("France").Add "Paris", 200
inhabitants_in("Italy").Add "Roma", 300
inhabitants_in("Italy").Add "Milano", 400
inhabitants_in("Italy").Add "Firenze", 500
inhabitants_in("Italy").Add "Venezia", 600
inhabitants_in("Norway")("Oslo") = 800
MsgBox (inhabitants_in("Norway")("Hardanger"))
inhabitants_in("Norway").Add "Hardanger", 111600  '<<< This line fails
MsgBox (inhabitants_in("Norway")("Hadanger"))
' Show values
MsgBox _
    inhabitants_in("Italy")("Venezia") & vbNewLine & _
    inhabitants_in("France")("Paris") & vbNewLine & _
    inhabitants_in("Norway")("Oslo")
    
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Your problem is the MsgBox line above it. That tries to print the value (item) of inhabitants_in("Norway")("Hardanger"), which doesn't exist at that time, but trying to reference it causes it to be created. So when you try to add it on the next line, it says you have a duplicate key.

You can actually use this behavior to your advantage. Instead of using .Add, you can do this:

inhabitants_in("Norway")("Hardanger") = 111600

If that key exists, it will update it. If that key does not exist, it will create it and update the item.

If you just want to see if the key exists, without creating it, use .Exists instead.

(It also appears you misspelled Hardanger in the next line.)

Hope this helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

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