Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default 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" wrote in message
oups.com...
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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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" wrote in message
oups.com...
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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default 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" wrote in message
oups.com...
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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default 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" wrote in message
oups.com...
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" wrote in message
oups.com...
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Customizing a dictionary Grant Excel Worksheet Functions 3 September 10th 09 01:22 PM
HELP-Dictionary dfisch8 Excel Programming 3 August 22nd 05 08:27 PM
dictionary Libby Excel Programming 0 November 15th 04 12:53 PM
Dictionary objet Zwi Excel Programming 2 January 26th 04 01:57 PM


All times are GMT +1. The time now is 11:09 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"