## STRATEGY with dictionary

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### STRATEGY with dictionary

i loop a txt line by line and get a sting (VALORE) with mid statement, during the loop, i fill a Dictionary whit:
...
If Not DICT.Exists(VALORE) Then
CONTA = CONTA + 1
End If
...

but my prob is to get a other string value for 9 other value!

i need to set 9 dictionary?

for example:

Set DICT1 = CreateObject("scripting.Dictionary")
Set DICT2 = CreateObject("scripting.Dictionary")
Set DICT3 = CreateObject("scripting.Dictionary")
...
Set DICT9 = CreateObject("scripting.Dictionary")

If Not DICT1.Exists(VALORE1) Then
CONTA1 = CONTA1 + 1
End If

If Not DICT2.Exists(VALORE2) Then
CONTA1 = CONTA1 + 1
End If

If Not DICT3.Exists(VALORE3) Then
CONTA1 = CONTA1 + 1
End If

...

If Not DICT9.Exists(VALORE9) Then
CONTA1 = CONTA1 + 1
End If

help!

HansV
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

### Re: STRATEGY with dictionary

No, I don't think so. If VALORE can have 9 different values, the code

Code: Select all

``````        If Not DICT.Exists(VALORE) Then
CONTA = CONTA + 1
End If
``````
will produce a dictionary with 9 items, one for each unique value of VALORE.
Regards,
Hans

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### Re: STRATEGY with dictionary

HansV wrote:
02 May 2021, 09:20
No, I don't think so. If VALORE can have 9 different values, the code

Code: Select all

``````        If Not DICT.Exists(VALORE) Then
CONTA = CONTA + 1
End If
``````
will produce a dictionary with 9 items, one for each unique value of VALORE.
hummm...
but to the and of looping, i need to fill a 9 different combobox with unique value of valore, valore1... valore9...

for example combobox1 for valore, combobox2 for valore1..., combobox9 for valore9

HansV
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

### Re: STRATEGY with dictionary

I'm afraid I don't understand.
Regards,
Hans

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### Re: STRATEGY with dictionary

in mid(mystring, 1,25) position have a value for VALORE i need to fill combobox1 with unique value of VALORE
in mid(mystring, 4,9) position have a value for VALORE1 i need to fill combobox2 with unique value of VALORE1
...
in mid(mystring, ,32) position have a value for VALORE9 i need to fill combobox9 with unique value of VALORE9

HansV
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

### Re: STRATEGY with dictionary

OK, thanks. You will need to create 9 dictionaries for that, or an array of type Dictionary with 9 elements.
Regards,
Hans

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### Re: STRATEGY with dictionary

...
or an array of type Dictionary with 9 elements
have an example?

HansV
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

### Re: STRATEGY with dictionary

Code: Select all

``````    Dim Arr(1 To 9) As Object
Dim i As Long
For i = 1 To 9
Set Arr(i) = CreasteObject(Class:="Scripting.Dictionary")
Next i

'...

If Not Arr(1).Exists(VALORE1) Then
CONTA1 = CONTA1 + 1
End If

If Not Arr(2).Exists(VALORE2) Then
CONTA2 = CONTA2 + 1
End If

' etc.``````
Regards,
Hans

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### Re: STRATEGY with dictionary

HansV wrote:
02 May 2021, 10:55

Code: Select all

``````    Dim Arr(1 To 9) As Object
Dim i As Long
For i = 1 To 9
Set Arr(i) = CreasteObject(Class:="Scripting.Dictionary")
Next i

'...

If Not Arr(1).Exists(VALORE1) Then
CONTA1 = CONTA1 + 1
End If

If Not Arr(2).Exists(VALORE2) Then
CONTA2 = CONTA2 + 1
End If

' etc.``````
i need to set to 0 (Zero) before start the looping in txt?

for exampe:
CONTA1=0
CONTA2==
...
CONTA9=0

or not?

HansV
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

### Re: STRATEGY with dictionary

No, that isn't necessary.
As the code is now, it'll set CONTA1 to 0 in Arr(1).Add Key:=VALORE1, Item:=CONTA1, etc.
Regards,
Hans

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### Re: STRATEGY with dictionary

HansV wrote:
02 May 2021, 14:45
No, that isn't necessary.
As the code is now, it'll set CONTA1 to 0 in Arr(1).Add Key:=VALORE1, Item:=CONTA1, etc.
ahhhhh...
tks

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### Re: STRATEGY with dictionary

HansV wrote:
02 May 2021, 14:45
No, that isn't necessary.
As the code is now, it'll set CONTA1 to 0 in Arr(1).Add Key:=VALORE1, Item:=CONTA1, etc.
And to the end of looping txt, how to loop each dictionary elements?

NOTE:
CreasteObject

s

HansV
Posts: 71155
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

### Re: STRATEGY with dictionary

For example:

Code: Select all

``````    Dim i As Long
Dim MyKey As Variant
For i = 1 to 9
For Each MyKey In Arr(i).Keys
Debug.Print MyKey
Next MyKey
Next i``````
Regards,
Hans

sal21
PlatinumLounger
Posts: 3556
Joined: 26 Apr 2010, 17:36

### Re: STRATEGY with dictionary

HansV wrote:
03 May 2021, 10:36
For example:

Code: Select all

``````    Dim i As Long
Dim MyKey As Variant
For i = 1 to 9
For Each MyKey In Arr(i).Keys
Debug.Print MyKey
Next MyKey
Next i``````