PC Review


Reply
Thread Tools Rate Thread

Defining Variable Type for Items and Keys in Dictionary

 
 
=?Utf-8?B?RXhjZWxNb25rZXk=?=
Guest
Posts: n/a
 
      4th May 2007
In the routine below I do not have Option Explicit turned off. When I turn
it on, I get error messages telling me that "Keys" and "Items" are not
defined. What do define them as?

Sub DictionaryDemo()
Dim d as Dictionary
Dim SearchTerm As String
Dim X As Integer

Set d = CreateObject("Scripting.Dictionary")

d.Add "Rob", "Athens" ' Add some keys and items.
d.Add "Barry", "Belgrade"
d.Add "Tim", "Cairo"
d.Add "Tom", "Edmonton"
d.Add "Sue", "Calgary"
d.Add "Sherry", "Vancouver"
d.Add "Brian", "Toronto"
d.Add "Anne", "New Jersey"
d.Add "Dave", "New York"
d.Add "Rick", "Houston"

Items = d.Items
Keys = d.Keys

SearchTerm = "Rob"

Debug.Print "Arrray contains the following data:"

For X = 0 To UBound(Keys)
If Keys(X) = "Rob" Then
Debug.Print Keys(X) & ": " & Items(X)
End If
Next
End Sub

Thanks
EM
 
Reply With Quote
 
 
 
 
RB Smissaert
Guest
Posts: n/a
 
      4th May 2007
Set a reference to the MS Scripting run time and see if this code helps:

Sub test()

Dim i As Long
Dim oDict As Scripting.Dictionary

Set oDict = New Scripting.Dictionary

For i = 1 To 10
oDict.Add Key:=i + 1, Item:=i
Next i

MsgBox oDict.Exists(11), , "key 11 exists?"

End Sub


RBS


"ExcelMonkey" <(E-Mail Removed)> wrote in message
news:5F1004C7-26F4-4579-BFE3-(E-Mail Removed)...
> In the routine below I do not have Option Explicit turned off. When I
> turn
> it on, I get error messages telling me that "Keys" and "Items" are not
> defined. What do define them as?
>
> Sub DictionaryDemo()
> Dim d as Dictionary
> Dim SearchTerm As String
> Dim X As Integer
>
> Set d = CreateObject("Scripting.Dictionary")
>
> d.Add "Rob", "Athens" ' Add some keys and items.
> d.Add "Barry", "Belgrade"
> d.Add "Tim", "Cairo"
> d.Add "Tom", "Edmonton"
> d.Add "Sue", "Calgary"
> d.Add "Sherry", "Vancouver"
> d.Add "Brian", "Toronto"
> d.Add "Anne", "New Jersey"
> d.Add "Dave", "New York"
> d.Add "Rick", "Houston"
>
> Items = d.Items
> Keys = d.Keys
>
> SearchTerm = "Rob"
>
> Debug.Print "Arrray contains the following data:"
>
> For X = 0 To UBound(Keys)
> If Keys(X) = "Rob" Then
> Debug.Print Keys(X) & ": " & Items(X)
> End If
> Next
> End Sub
>
> Thanks
> EM


 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      4th May 2007
Hello Monkey,

Sub DictionaryDemo()
Dim d as Dictionary
Dim SearchTerm As String
Dim X As Integer
Dim Keys as Variant
Dim Items as Variant

--
Regards,
Tom Ogilvy


"ExcelMonkey" wrote:

> In the routine below I do not have Option Explicit turned off. When I turn
> it on, I get error messages telling me that "Keys" and "Items" are not
> defined. What do define them as?
>
> Sub DictionaryDemo()
> Dim d as Dictionary
> Dim SearchTerm As String
> Dim X As Integer
>
> Set d = CreateObject("Scripting.Dictionary")
>
> d.Add "Rob", "Athens" ' Add some keys and items.
> d.Add "Barry", "Belgrade"
> d.Add "Tim", "Cairo"
> d.Add "Tom", "Edmonton"
> d.Add "Sue", "Calgary"
> d.Add "Sherry", "Vancouver"
> d.Add "Brian", "Toronto"
> d.Add "Anne", "New Jersey"
> d.Add "Dave", "New York"
> d.Add "Rick", "Houston"
>
> Items = d.Items
> Keys = d.Keys
>
> SearchTerm = "Rob"
>
> Debug.Print "Arrray contains the following data:"
>
> For X = 0 To UBound(Keys)
> If Keys(X) = "Rob" Then
> Debug.Print Keys(X) & ": " & Items(X)
> End If
> Next
> End Sub
>
> Thanks
> EM

 
Reply With Quote
 
Susan
Guest
Posts: n/a
 
      4th May 2007

Dim Items as ????????????
Dim Keys as ????????????

i think that's what it's looking for.
susan

On May 4, 1:15 pm, ExcelMonkey <ExcelMon...@discussions.microsoft.com>
wrote:
> In the routine below I do not have Option Explicit turned off. When I turn
> it on, I get error messages telling me that "Keys" and "Items" are not
> defined. What do define them as?
>
> Sub DictionaryDemo()
> Dim d as Dictionary
> Dim SearchTerm As String
> Dim X As Integer
>
> Set d = CreateObject("Scripting.Dictionary")
>
> d.Add "Rob", "Athens" ' Add some keys and items.
> d.Add "Barry", "Belgrade"
> d.Add "Tim", "Cairo"
> d.Add "Tom", "Edmonton"
> d.Add "Sue", "Calgary"
> d.Add "Sherry", "Vancouver"
> d.Add "Brian", "Toronto"
> d.Add "Anne", "New Jersey"
> d.Add "Dave", "New York"
> d.Add "Rick", "Houston"
>
> Items = d.Items
> Keys = d.Keys
>
> SearchTerm = "Rob"
>
> Debug.Print "Arrray contains the following data:"
>
> For X = 0 To UBound(Keys)
> If Keys(X) = "Rob" Then
> Debug.Print Keys(X) & ": " & Items(X)
> End If
> Next
> End Sub
>
> Thanks
> EM



 
Reply With Quote
 
=?Utf-8?B?RXhjZWxNb25rZXk=?=
Guest
Posts: n/a
 
      4th May 2007
Yes Tom they needed to be dimensioned as Variants. Wasn't sure what data
type to use. Was originaly thinking it was going to be
"Dictionary.Something" but nothing was coming up in the itellisense.

Thanks

"Tom Ogilvy" wrote:

> Hello Monkey,
>
> Sub DictionaryDemo()
> Dim d as Dictionary
> Dim SearchTerm As String
> Dim X As Integer
> Dim Keys as Variant
> Dim Items as Variant
>
> --
> Regards,
> Tom Ogilvy
>
>
> "ExcelMonkey" wrote:
>
> > In the routine below I do not have Option Explicit turned off. When I turn
> > it on, I get error messages telling me that "Keys" and "Items" are not
> > defined. What do define them as?
> >
> > Sub DictionaryDemo()
> > Dim d as Dictionary
> > Dim SearchTerm As String
> > Dim X As Integer
> >
> > Set d = CreateObject("Scripting.Dictionary")
> >
> > d.Add "Rob", "Athens" ' Add some keys and items.
> > d.Add "Barry", "Belgrade"
> > d.Add "Tim", "Cairo"
> > d.Add "Tom", "Edmonton"
> > d.Add "Sue", "Calgary"
> > d.Add "Sherry", "Vancouver"
> > d.Add "Brian", "Toronto"
> > d.Add "Anne", "New Jersey"
> > d.Add "Dave", "New York"
> > d.Add "Rick", "Houston"
> >
> > Items = d.Items
> > Keys = d.Keys
> >
> > SearchTerm = "Rob"
> >
> > Debug.Print "Arrray contains the following data:"
> >
> > For X = 0 To UBound(Keys)
> > If Keys(X) = "Rob" Then
> > Debug.Print Keys(X) & ": " & Items(X)
> > End If
> > Next
> > End Sub
> >
> > Thanks
> > EM

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      4th May 2007
these properties are passed as arrays, so a variant is needed.

--
Regards,
Tom Ogilvy


"ExcelMonkey" wrote:

> Yes Tom they needed to be dimensioned as Variants. Wasn't sure what data
> type to use. Was originaly thinking it was going to be
> "Dictionary.Something" but nothing was coming up in the itellisense.
>
> Thanks
>
> "Tom Ogilvy" wrote:
>
> > Hello Monkey,
> >
> > Sub DictionaryDemo()
> > Dim d as Dictionary
> > Dim SearchTerm As String
> > Dim X As Integer
> > Dim Keys as Variant
> > Dim Items as Variant
> >
> > --
> > Regards,
> > Tom Ogilvy
> >
> >
> > "ExcelMonkey" wrote:
> >
> > > In the routine below I do not have Option Explicit turned off. When I turn
> > > it on, I get error messages telling me that "Keys" and "Items" are not
> > > defined. What do define them as?
> > >
> > > Sub DictionaryDemo()
> > > Dim d as Dictionary
> > > Dim SearchTerm As String
> > > Dim X As Integer
> > >
> > > Set d = CreateObject("Scripting.Dictionary")
> > >
> > > d.Add "Rob", "Athens" ' Add some keys and items.
> > > d.Add "Barry", "Belgrade"
> > > d.Add "Tim", "Cairo"
> > > d.Add "Tom", "Edmonton"
> > > d.Add "Sue", "Calgary"
> > > d.Add "Sherry", "Vancouver"
> > > d.Add "Brian", "Toronto"
> > > d.Add "Anne", "New Jersey"
> > > d.Add "Dave", "New York"
> > > d.Add "Rick", "Houston"
> > >
> > > Items = d.Items
> > > Keys = d.Keys
> > >
> > > SearchTerm = "Rob"
> > >
> > > Debug.Print "Arrray contains the following data:"
> > >
> > > For X = 0 To UBound(Keys)
> > > If Keys(X) = "Rob" Then
> > > Debug.Print Keys(X) & ": " & Items(X)
> > > End If
> > > Next
> > > End Sub
> > >
> > > Thanks
> > > EM

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subject: Type 13 Error when defining a Columns variable MikeZz Microsoft Excel Programming 2 14th Apr 2009 09:04 PM
Re: Subject: Type 13 Error when defining a Columns variable Per Jessen Microsoft Excel Programming 0 14th Apr 2009 08:06 PM
Problem with Dictionary<T,U>.Keys enumerator returning invalid keys Brian Richards Microsoft C# .NET 5 5th Jun 2006 11:56 PM
Problem with Dictionary<T,U>.Keys enumerator returning invalid keys Brian Richards Microsoft Dot NET 1 5th Jun 2006 11:54 PM
Adding user type variable to dictionary Jonathan Blitz Microsoft Access VBA Modules 1 30th May 2004 07:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:05 PM.