Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:46:10+00:00 2026-06-06T21:46:10+00:00

Similar to this issue , when using a Scripting.Dictionary object in VBA, the outcome

  • 0

Similar to this issue, when using a Scripting.Dictionary object in VBA, the outcome of the code below is unexpected.

Option Explicit

Sub test()

    Dim d As Variant
    Dim i As Integer
    Dim s As String
    Set d = CreateObject("Scripting.Dictionary")

    d.Add "a", "a"
    Debug.Print d.Count ' Prints '1' as expected

    For i = 1 To d.Count
        s = d.Item(i)
        Debug.Print s ' Prints ' ' (null) instead of 'a'
    Next i

    Debug.Print d.Count ' Prints '2' instead of '1'

End Sub

Using a zero-based index, the same outcome is achieved:

For i = 0 To d.Count - 1
    s = d.Item(i)
    Debug.Print s
Next i

Watching the object, I can actually see that it has two items, the key for the newly added is 1, as added from i. If I increase this loop to a higher number, then the number of items in the dictionary is increased, once for each loop.

I have tested this in Office/VBA 2003, 2010, and 2013. All exhibit the same behavior, and I expect other versions (2007) will as well.

I can work around this with other looping methods, but this caught me off guard when I was trying to store objects and was getting an object expected error on the s = d.Item(i) line.

For the record, I know that I can do things like this:

For Each v In d.Keys
    Set o = d.item(v)
Next v

But I’m more curious about why I can’t seem to iterate through the items by number.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:46:11+00:00Added an answer on June 6, 2026 at 9:46 pm

    According to the documentation of the Item property:

    Sets or returns an item for a specified key in a Dictionary object.

    In your case, you don’t have an item whose key is 1 so doing:

    s = d.Item(i)
    

    actually creates a new key / value pair in your dictionary, and the value is empty because you have not used the optional newItem argument.

    The Dictionary also has the Items method which allows looping over the indices:

    a = d.Items
    For i = 0 To d.Count - 1
        s = a(i)
    Next i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My issue is very similar to this issue. However, I'm using SQL Server 2005
using code similar to this Inno Setup for Windows service? on a windows 7
I've been reading similar questions to this issue and have been able to get
I can't resolve this issue, I found a similar question here but: setting the
my issue lokks similar to this one: (link) but i have one-to-many association: <set
An issue I discover today is similar to this unanswered problem ; though not
Jquery Each Json Values Issue This question is similar to above, but not the
I know that there are some threads have a similar issue with this thread.
I have been through numerous similar questions on this forum but my issue still
Similar to this question I am using a custom VirtualPathProvider to retrieve views that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.