Consulting

Results 1 to 4 of 4

Thread: Call previous dictionary entry

  1. #1
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location

    Call previous dictionary entry

    I have a section of code that builds a dictionary (def) from a text document. The tempdef is an array that is built from readline of the .txt and has the spaces filtered out after a split of the line. Tempdef captures the entire line of text on the current line in the .txt document, and is then assigned to a dictionary entry. The sections of the document have a couple rows of titles (found with isnumeric), then gets into the data. The data has two rows per element and I'd like to find out how to name the second row relative to the last entry in the dictionary, which would be the corresponding element. The text file would look something like this (row #'s for clarification):

    1) element ID title title title
    2) 22512 .025 33.33 225.55
    3) 0 0 0 1102

    The next element would then follow in this same format. Rows 2 and 3 pertain to the same element #. Currently the dictionary key is defined as the element ID and I'd like to make the key for the second row 'element ID & a', so for the data above, row 2 key would be 22512, row 3 key would be 22512a.

    I've provided my current code that uses a separate keyarray (keyarray = def.keys), but this is terribly slow (This was just something I was messing with). I'm wondering if there is a way to call the previous item in the dictionary or if there's a way to call an item in the dictionary by it's "item #" that shows up in the watches window. Tempdef is cleared when the loop moves to the next line of the .txt document, so I am unable to call the previous tempdef data.

    Ideally, something like def.item(i -1) is what I'm looking for, i being a counter, but I know this does not work.

    I'm fairly new to VBA, so any help is appreciated!

    'Title values
                If Not IsNumeric(tempdef(1)) Then
                    If Not def.Exists(i) Then
                        def.Add i, tempdef
                    End If
                Else
    'Data values
                    If Not def.Exists(CLng(tempdef(1))) Then
    '                        ReDim Preserve keyarray(1 To def.count)
    '                        keyarray = def.Keys
    '                        def.Add CLng((keyarray(i - 2)) & Asc("a")), tempdef
                    End If
                End If
    Last edited by khu; 03-03-2014 at 12:33 PM.

  2. #2
    row 3 key would be 22512a.
    after the first item, try
    "22512" & chr(97 + i)
    of course, you run out of letters after 26 lines

  3. #3
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location
    Quote Originally Posted by westconn1 View Post
    after the first item, try
    "22512" & chr(97 + i)
    of course, you run out of letters after 26 lines


    I have thousands of data points that I will be dealing with and need each line identified by its element ID later on, so I need a way to append asc("a") to the previous line's element ID.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Almost everything about dictionaries:

    http://www.snb-vba.eu/VBA_Dictionary_en.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •