PC Review


Reply
Thread Tools Rate Thread

dictionary count is greater than number of keys?

 
 
thorgan
Guest
Posts: n/a
 
      18th Aug 2008
Dear Group,

I am completely stumped by this. I am filling a dictionary and then
looping over an array made from its keys later. However, if I loop to
the count size of the dictionary I will hit an error. The reason seems
to be that the array that is made via the keys method, is one element
shorter than the dictionary count?

Sample code below:
Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' INPUTS HERE ''
Public Const distance As Single = 26.1
Public lrods As Dictionary
Public lrods_1 As Dictionary
Public lrods_9 As Dictionary
Public worst As Integer
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Sub Identify_lrods()
Dim ws As Worksheet
Dim i As Integer
Set lrods = New Dictionary
Set lrods_1 = New Dictionary
Set lrods_9 = New Dictionary

'look at sheet 3
Set ws = ThisWorkbook.Worksheets("3")
'loop through cells till empty
i = 0

Do While IsEmpty(ws.Range("J" & 3 + i)) <> True

If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B" & 3
+ i) = 1 Then _
lrods_1.Add ws.Range("J" & CStr(3 + i)), ws.Range("P" & CStr(3
+ i))

If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B" & 3
+ i) = 9 Then _
lrods_9.Add ws.Range("J" & CStr(3 + i)), ws.Range("P" & CStr(3
+ i))

i = i + 2 'skip every second
Loop

Debug.Print "after filling dictionaries - the i value was", i - 2
Debug.Print "dictionary info", lrods_9.Count, UBound(lrods_9.Keys)

End Sub

Many thanks,
Tim
 
Reply With Quote
 
 
 
 
thorgan
Guest
Posts: n/a
 
      18th Aug 2008
On Aug 18, 11:36*am, thorgan <timmy.hor...@gmail.com> wrote:
> Dear Group,
>
> I am completely stumped by this. I am filling a dictionary and then
> looping over an array made from its keys later. However, if I loop to
> the count size of the dictionary I will hit an error. The reason seems
> to be that the array that is made via the keys method, is one element
> shorter than the dictionary count?
>
> Sample code below:
> Option Explicit
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> '' *INPUTS HERE ''
> Public Const distance As Single = 26.1
> Public lrods As Dictionary
> Public lrods_1 As Dictionary
> Public lrods_9 As Dictionary
> Public worst As Integer
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>
> Sub Identify_lrods()
> Dim ws As Worksheet
> Dim i As Integer
> Set lrods = New Dictionary
> Set lrods_1 = New Dictionary
> Set lrods_9 = New Dictionary
>
> 'look at sheet 3
> Set ws = ThisWorkbook.Worksheets("3")
> 'loop through cells till empty
> i = 0
>
> Do While IsEmpty(ws.Range("J" & 3 + i)) <> True
>
> * * If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B" & 3
> + i) = 1 Then _
> * * * * lrods_1.Add ws.Range("J" & CStr(3 + i)), ws.Range("P" & CStr(3
> + i))
>
> * * If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B" & 3
> + i) = 9 Then _
> * * * * lrods_9.Add ws.Range("J" & CStr(3 + i)), ws.Range("P" & CStr(3
> + i))
>
> * * i = i + 2 'skip every second
> Loop
>
> Debug.Print "after filling dictionaries - the i value was", i - 2
> Debug.Print "dictionary info", lrods_9.Count, UBound(lrods_9.Keys)
>
> End Sub
>
> Many thanks,
> Tim


UPDATE:

A colleague figured this out - the arrays created from dictionaries
seem to start their index at zero. However, something odd came of
that. Later the .keys array was assigned to an array dimensioned as (1
to dictionary.count). No errors occurred, but it seems that the array
was shifted - i.e. filled with, say, 15 keys, but array(15) would
return an error. Odd.

Any thoughts?

Tim
 
Reply With Quote
 
Barb Reinhardt
Guest
Posts: n/a
 
      18th Aug 2008
What do Array(0) through Array(14) return?
--
HTH,
Barb Reinhardt



"thorgan" wrote:

> On Aug 18, 11:36 am, thorgan <timmy.hor...@gmail.com> wrote:
> > Dear Group,
> >
> > I am completely stumped by this. I am filling a dictionary and then
> > looping over an array made from its keys later. However, if I loop to
> > the count size of the dictionary I will hit an error. The reason seems
> > to be that the array that is made via the keys method, is one element
> > shorter than the dictionary count?
> >
> > Sample code below:
> > Option Explicit
> >
> > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > '' INPUTS HERE ''
> > Public Const distance As Single = 26.1
> > Public lrods As Dictionary
> > Public lrods_1 As Dictionary
> > Public lrods_9 As Dictionary
> > Public worst As Integer
> > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> >
> > Sub Identify_lrods()
> > Dim ws As Worksheet
> > Dim i As Integer
> > Set lrods = New Dictionary
> > Set lrods_1 = New Dictionary
> > Set lrods_9 = New Dictionary
> >
> > 'look at sheet 3
> > Set ws = ThisWorkbook.Worksheets("3")
> > 'loop through cells till empty
> > i = 0
> >
> > Do While IsEmpty(ws.Range("J" & 3 + i)) <> True
> >
> > If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B" & 3
> > + i) = 1 Then _
> > lrods_1.Add ws.Range("J" & CStr(3 + i)), ws.Range("P" & CStr(3
> > + i))
> >
> > If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B" & 3
> > + i) = 9 Then _
> > lrods_9.Add ws.Range("J" & CStr(3 + i)), ws.Range("P" & CStr(3
> > + i))
> >
> > i = i + 2 'skip every second
> > Loop
> >
> > Debug.Print "after filling dictionaries - the i value was", i - 2
> > Debug.Print "dictionary info", lrods_9.Count, UBound(lrods_9.Keys)
> >
> > End Sub
> >
> > Many thanks,
> > Tim

>
> UPDATE:
>
> A colleague figured this out - the arrays created from dictionaries
> seem to start their index at zero. However, something odd came of
> that. Later the .keys array was assigned to an array dimensioned as (1
> to dictionary.count). No errors occurred, but it seems that the array
> was shifted - i.e. filled with, say, 15 keys, but array(15) would
> return an error. Odd.
>
> Any thoughts?
>
> Tim
>

 
Reply With Quote
 
thorgan
Guest
Posts: n/a
 
      18th Aug 2008
On Aug 18, 1:20*pm, Barb Reinhardt
<BarbReinha...@discussions.microsoft.com> wrote:
> What do Array(0) through Array(14) return?
> --
> HTH,
> Barb Reinhardt
>
>
>
> "thorgan" wrote:
> > On Aug 18, 11:36 am, thorgan <timmy.hor...@gmail.com> wrote:
> > > Dear Group,

>
> > > I am completely stumped by this. I am filling a dictionary and then
> > > looping over an array made from its keys later. However, if I loop to
> > > the count size of the dictionary I will hit an error. The reason seems
> > > to be that the array that is made via the keys method, is one element
> > > shorter than the dictionary count?

>
> > > Sample code below:
> > > Option Explicit

>
> > > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > > '' *INPUTS HERE ''
> > > Public Const distance As Single = 26.1
> > > Public lrods As Dictionary
> > > Public lrods_1 As Dictionary
> > > Public lrods_9 As Dictionary
> > > Public worst As Integer
> > > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> > > ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

>
> > > Sub Identify_lrods()
> > > Dim ws As Worksheet
> > > Dim i As Integer
> > > Set lrods = New Dictionary
> > > Set lrods_1 = New Dictionary
> > > Set lrods_9 = New Dictionary

>
> > > 'look at sheet 3
> > > Set ws = ThisWorkbook.Worksheets("3")
> > > 'loop through cells till empty
> > > i = 0

>
> > > Do While IsEmpty(ws.Range("J" & 3 + i)) <> True

>
> > > * * If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B"& 3
> > > + i) = 1 Then _
> > > * * * * lrods_1.Add ws.Range("J" & CStr(3 + i)), ws.Range("P"& CStr(3
> > > + i))

>
> > > * * If ws.Range("P" & 3 + i) > (distance / 1000) And ws.Range("B"& 3
> > > + i) = 9 Then _
> > > * * * * lrods_9.Add ws.Range("J" & CStr(3 + i)), ws.Range("P"& CStr(3
> > > + i))

>
> > > * * i = i + 2 'skip every second
> > > Loop

>
> > > Debug.Print "after filling dictionaries - the i value was", i - 2
> > > Debug.Print "dictionary info", lrods_9.Count, UBound(lrods_9.Keys)

>
> > > End Sub

>
> > > Many thanks,
> > > Tim

>
> > UPDATE:

>
> > A colleague figured this out - the arrays created from dictionaries
> > seem to start their index at zero. However, something odd came of
> > that. Later the .keys array was assigned to an array dimensioned as (1
> > to dictionary.count). No errors occurred, but it seems that the array
> > was shifted - i.e. filled with, say, 15 keys, but array(15) would
> > return an error. Odd.

>
> > Any thoughts?

>
> > Tim- Hide quoted text -

>
> - Show quoted text -


Hi Barb,

thanks for your interest. The array elements are now correctly filled
with the keys. I had defined the array to be array(1 to 15), rather
than dim array(14). So I didn't think that after the
array=dictionary.keys assignment it would work as the latter rather
than the former (or the former with an error).

Kind regards,
Tim
 
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
count the number of rows with entries greater than zero r2rcode Microsoft Excel Worksheet Functions 1 17th May 2010 05:35 PM
Pie Chart report based off count of items less than a number and greater than a number NeoFax99@gmail.com Microsoft Access 2 5th Dec 2006 12:14 PM
Count of Number Values Greater Than, Less Than =?Utf-8?B?QW5uZQ==?= Microsoft Excel Worksheet Functions 6 12th Oct 2006 07:04 PM
Problem with Dictionary<T,U>.Keys enumerator returning invalid keys Brian Richards Microsoft C# .NET 5 5th Jun 2006 11:56 PM
count a number that is less than a number and greater than a numbe =?Utf-8?B?RGFsZQ==?= Microsoft Excel Worksheet Functions 3 19th Nov 2005 05:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:08 AM.