Tree structure by Object

PeterBunde

New Member
Joined
Dec 7, 2016
Messages
45
Fellow sufferers

Please see my (partly pseudo-) code below. I first build a tree structure and would then like to pick one branch of it and send it to a subroutine for processing. Believe the code explains what I wish to do.

BW Peter Bunde Hansen

Code:
Sub Test()
Dim inhabitants_in As Object
Dim x As Variant
Dim y As Variant
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", 500000
inhabitants_in("Norway").Add "Bergen", 300000
inhabitants_in("France").Add "Paris", 1000000
For Each country In inhabitants_in
  
    Call country_manager(country)
    
Next country
   
End Sub

Private Function country_manager(cities_population() As Integer)
For Each city In cities_population
  
    MsgBox ("Poopulation of: " & CStr(city) & " is: " & city_population(city))
    
Next country
End Function
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You need to look up how to loop through dictionaries:
Code:
Sub Test()
Dim inhabitants_in As Object
Dim x As Variant
Dim y As Variant
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", 500000
inhabitants_in("Norway").Add "Bergen", 300000
inhabitants_in("France").Add "Paris", 1000000
For Each country In inhabitants_in.keys
  
    Call country_manager(inhabitants_in(country))
    
Next country
   
End Sub

Private Function country_manager(cities_population As Object)

For Each city In cities_population.keys
  
    Debug.Print "Poopulation of: " & city & " is: " & cities_population(city)
    
Next city
End Function
 
Upvote 0
Please comply with the forum rules. It would be unfortunate if I have to remind you again.
 
Upvote 0
Perhaps:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG28Sep45
[COLOR="Navy"]Dim[/COLOR] inhabitants_in [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] x [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] y [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Country [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] City [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] St [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] AllStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] inhabitants_in = CreateObject("Scripting.Dictionary")
'[COLOR="Green"][B] add countries[/B][/COLOR]
inhabitants_in.Add "Norway", CreateObject("Scripting.Dictionary")
inhabitants_in.Add "France", CreateObject("Scripting.Dictionary")
inhabitants_in.Add "Italy", CreateObject("Scripting.Dictionary")
inhabitants_in.Add "UK", CreateObject("Scripting.Dictionary")
'[COLOR="Green"][B] add cities and number of inhabitants[/B][/COLOR]
inhabitants_in("Norway").Add "Oslo", 500000
inhabitants_in("Norway").Add "Bergen", 300000
inhabitants_in("France").Add "Paris", 1000000
inhabitants_in("Italy").Add "Turin", 1500000
inhabitants_in("UK").Add "London", 1550000
[COLOR="Navy"]For[/COLOR] n = 0 To inhabitants_in.Count - 1
    nStr = nStr & "(" & n + 1 & ") " & inhabitants_in.keys()(n) & vbCrLf
[COLOR="Navy"]Next[/COLOR] n
Num = Application.InputBox(prompt:="Please enter Country Number " & vbCrLf & nStr, Title:="Cities", Type:=1)
AllStr = ""
[COLOR="Navy"]If[/COLOR] Num > 0 And Num < inhabitants_in.Count + 1 [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Country [COLOR="Navy"]In[/COLOR] inhabitants_in.keys
        [COLOR="Navy"]If[/COLOR] Country = inhabitants_in.keys()(Num - 1) [COLOR="Navy"]Then[/COLOR]
            AllStr = "Country :- " & Country
            [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] City [COLOR="Navy"]In[/COLOR] inhabitants_in(Country)
               AllStr = AllStr & vbCrLf & "City :- " & City & vbLf & "Population :-" & inhabitants_in(Country)(City)
            [COLOR="Navy"]Next[/COLOR] City
       [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Country
[COLOR="Navy"]End[/COLOR] If
MsgBox AllStr
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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