+ Reply to Thread
Results 1 to 5 of 5

looping through items in a dictionary

  1. #1
    Abe
    Guest

    looping through items in a dictionary

    I have a dictionary, named dictCornerCellPics, whose items are all
    picture objects. I want to loop through each picture's name (not key!)
    and if it satisifies a condition, the code does something. This below
    gives the gist of what I want to do, but doesn't work.

    dim pic as Picture
    dim picName as string

    For each pic in dictCornerCellPics
    picName = pic.name
    if picName like "UL*" then
    'does some stuff
    end if
    next pic

    This code also returns an error if I use dictCornerCellPics

    The code above returns an "Object Required" error. Anybody know what's
    wrong with the above code, or can give a better/different way to
    achieve the same thing?


  2. #2
    Bob Phillips
    Guest

    Re: looping through items in a dictionary

    Here is an example

    Dim dict As Object
    Dim ary
    Dim i As Long

    Set dict = CreateObject("Scripting.Dictionary")
    With dict
    .Add "a", "Athens" ' Add some keys and items.
    .Add "b", "Belgrade"
    .Add "c", "Cairo"
    End With

    ary = dict.Items

    For i = 0 To dict.Count - 1
    Debug.Print ary(i)
    Next


    --
    HTH

    Bob Phillips

    (replace somewhere in email address with gmail if mailing direct)

    "Abe" <[email protected]> wrote in message
    news:[email protected]...
    > I have a dictionary, named dictCornerCellPics, whose items are all
    > picture objects. I want to loop through each picture's name (not key!)
    > and if it satisifies a condition, the code does something. This below
    > gives the gist of what I want to do, but doesn't work.
    >
    > dim pic as Picture
    > dim picName as string
    >
    > For each pic in dictCornerCellPics
    > picName = pic.name
    > if picName like "UL*" then
    > 'does some stuff
    > end if
    > next pic
    >
    > This code also returns an error if I use dictCornerCellPics
    >
    > The code above returns an "Object Required" error. Anybody know what's
    > wrong with the above code, or can give a better/different way to
    > achieve the same thing?
    >




  3. #3
    Chip Pearson
    Guest

    Re: looping through items in a dictionary

    When you do a For Each on a dictionary object, you enumerate the
    Keys, not the Items. Yes, this is counter-intuitive. Change your
    code to

    For each pic in dictCornerCellPics.Items


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com



    "Abe" <[email protected]> wrote in message
    news:[email protected]...
    >I have a dictionary, named dictCornerCellPics, whose items are
    >all
    > picture objects. I want to loop through each picture's name
    > (not key!)
    > and if it satisifies a condition, the code does something.
    > This below
    > gives the gist of what I want to do, but doesn't work.
    >
    > dim pic as Picture
    > dim picName as string
    >
    > For each pic in dictCornerCellPics
    > picName = pic.name
    > if picName like "UL*" then
    > 'does some stuff
    > end if
    > next pic
    >
    > This code also returns an error if I use dictCornerCellPics
    >
    > The code above returns an "Object Required" error. Anybody know
    > what's
    > wrong with the above code, or can give a better/different way
    > to
    > achieve the same thing?
    >




  4. #4
    Abe
    Guest

    Re: looping through items in a dictionary

    Chip,

    I tried that and it still returned the object error. Any guesses as to
    why (I know my dictionary has been initialized, but it may
    not--depending on the user's inputs--have anything in it)?

    -Abe


    Chip Pearson wrote:
    > When you do a For Each on a dictionary object, you enumerate the
    > Keys, not the Items. Yes, this is counter-intuitive. Change your
    > code to
    >
    > For each pic in dictCornerCellPics.Items
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    >
    > "Abe" <[email protected]> wrote in message
    > news:[email protected]...
    > >I have a dictionary, named dictCornerCellPics, whose items are
    > >all
    > > picture objects. I want to loop through each picture's name
    > > (not key!)
    > > and if it satisifies a condition, the code does something.
    > > This below
    > > gives the gist of what I want to do, but doesn't work.
    > >
    > > dim pic as Picture
    > > dim picName as string
    > >
    > > For each pic in dictCornerCellPics
    > > picName = pic.name
    > > if picName like "UL*" then
    > > 'does some stuff
    > > end if
    > > next pic
    > >
    > > This code also returns an error if I use dictCornerCellPics
    > >
    > > The code above returns an "Object Required" error. Anybody know
    > > what's
    > > wrong with the above code, or can give a better/different way
    > > to
    > > achieve the same thing?
    > >



  5. #5
    Bob Phillips
    Guest

    Re: looping through items in a dictionary

    Because it doesn't work that way. See my previous post.

    --
    HTH

    Bob Phillips

    (replace somewhere in email address with gmail if mailing direct)

    "Abe" <[email protected]> wrote in message
    news:[email protected]...
    > Chip,
    >
    > I tried that and it still returned the object error. Any guesses as to
    > why (I know my dictionary has been initialized, but it may
    > not--depending on the user's inputs--have anything in it)?
    >
    > -Abe
    >
    >
    > Chip Pearson wrote:
    > > When you do a For Each on a dictionary object, you enumerate the
    > > Keys, not the Items. Yes, this is counter-intuitive. Change your
    > > code to
    > >
    > > For each pic in dictCornerCellPics.Items
    > >
    > >
    > > --
    > > Cordially,
    > > Chip Pearson
    > > Microsoft MVP - Excel
    > > Pearson Software Consulting, LLC
    > > www.cpearson.com
    > >
    > >
    > >
    > > "Abe" <[email protected]> wrote in message
    > > news:[email protected]...
    > > >I have a dictionary, named dictCornerCellPics, whose items are
    > > >all
    > > > picture objects. I want to loop through each picture's name
    > > > (not key!)
    > > > and if it satisifies a condition, the code does something.
    > > > This below
    > > > gives the gist of what I want to do, but doesn't work.
    > > >
    > > > dim pic as Picture
    > > > dim picName as string
    > > >
    > > > For each pic in dictCornerCellPics
    > > > picName = pic.name
    > > > if picName like "UL*" then
    > > > 'does some stuff
    > > > end if
    > > > next pic
    > > >
    > > > This code also returns an error if I use dictCornerCellPics
    > > >
    > > > The code above returns an "Object Required" error. Anybody know
    > > > what's
    > > > wrong with the above code, or can give a better/different way
    > > > to
    > > > achieve the same thing?
    > > >

    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1