loop trought dictionary collection

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

loop trought dictionary collection

Post by sal21 »

I use this code to fill dictionay....
Afetr i need to loop trought the 2 items in collection Key and item

note:
see in the code the part where is "???".

Code: Select all

Option Explicit
Dim I As Long, D_KEY As String, D_ITEM As String
Dim MYDICT As Dictionary, CONTA As Long
Dim nFileNum As Integer, SText As String, sNextLine As String, lLineCount As Long, MText As String
Sub TEST_DICTIONARY()

    Set MYDICT = New Dictionary

    nFileNum = FreeFile
    Open "C:\TEMP\TAB.TXT" For Input As nFileNum
    lLineCount = 0
    CONTA = 0
    Do While Not EOF(nFileNum)
        Line Input #nFileNum, sNextLine
        If Mid(sNextLine, 1, 17) = "CODICE" Then
            SText = Trim(Mid(sNextLine, 19, 4))
            MText = Trim(Mid(sNextLine, 38, 50))
            If MYDICT.Exists(SText) = False Then
                MYDICT.Add SText, MText
                CONTA = CONTA + 1
            End If
        End If
    Loop
    
    For I = 0 To MYDICT.Count - 1
    D_KEY = "???" < SText
    D_ITEM = "???" < MText
    Next I

    Close nFileNum
    Set MYDICT = Nothing

End Sub


User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop trought dictionary collection

Post by HansV »

I don't think this will work - the length of "CODICE" is only 6, so MID(sNextLine, 17) will not be equal to "CODICE". Try

If Mid(sNextLine, 1, 6) = "CODICE" Then

Instead of

Dim I As Long

use

Dim itm As Variant

and then use this loop:

Code: Select all

    For Each itm In MYDICT
        D_KEY = itm
        D_ITEM = MYDICT(itm)
        ...
    Next itm
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: loop trought dictionary collection

Post by sal21 »

HansV wrote:I don't think this will work - the length of "CODICE" is only 6, so MID(sNextLine, 17) will not be equal to "CODICE". Try

If Mid(sNextLine, 1, 6) = "CODICE" Then

Instead of

Dim I As Long

use

Dim itm As Variant

and then use this loop:

Code: Select all

    For Each itm In MYDICT
        D_KEY = itm
        D_ITEM = MYDICT(itm)
        ...
    Next itm
OK for all suggestion.... modified code and all work fine! :thankyou:

But out this post, i have see you indent your code e very well...

In my office in Vba excel ide i use Smart indenter to indent the code on a old pc.
On new pc where i not have Admin permission i cannot install nothing, exist a method to install on ide a indenter tool without instal .exe or dll?

ops! is possible to sort the dictionary items before to loop?

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop trought dictionary collection

Post by HansV »

Are you allowed to right-click an exe and select "Run as Administrator" from the context menu or is that blocked too?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: loop trought dictionary collection

Post by sal21 »

HansV wrote:Are you allowed to right-click an exe and select "Run as Administrator" from the context menu or is that blocked too?
no test this tips.... i can tomorrow

during you reply i have added a a request about sort..., sorry for that:-)

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop trought dictionary collection

Post by HansV »

See the SortDictionary procedure at the end of Collection And Dictionary Procedures on Chip Pearson's website.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: loop trought dictionary collection

Post by sal21 »

HansV wrote:See the SortDictionary procedure at the end of Collection And Dictionary Procedures on Chip Pearson's website.
ok...

but how to call the routine base my dictionary name

SortDictionary, ????,????,????

Public Sub SortDictionary(Dict As Scripting.Dictionary, _
SortByKey As Boolean, _
Optional Descending As Boolean = False, _
Optional CompareMode As VbCompareMethod = vbTextCompare)

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop trought dictionary collection

Post by HansV »

If you want to sort on the key SText:

SortDictionary MYDICT, True

If you want to sort on the value MText:

SortDictionary MYDICT, False
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: loop trought dictionary collection

Post by sal21 »

HansV wrote:If you want to sort on the key SText:

SortDictionary MYDICT, True

If you want to sort on the value MText:

SortDictionary MYDICT, False
:thankyou:

Now all work fine!

But the Dictionary object accepts only one Key and one Value or i can use one Key and many values for each Key?

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: loop trought dictionary collection

Post by HansV »

A dictionary item can be anything you want: a string, a number, an array or even a complete dictionary. Keep in mind that if the items aren't simple data such as a string or a number, you can only sort the dictionary by its key, not by its items.
Best wishes,
Hans