Items not being added to dictionary

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,436
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hi All,

I have the below which is supposed to be adding a range of text values to a dictionary however when I debug.print it nothing is being added. list1 is defined as a variant.

Code:
   Set d = CreateObject("scripting.dictionary")
list1 = ThisWorkbook.Sheets("Summary").Range("A7:A22").Value
For e = LBound(list1) To UBound(list1)
On Error Resume Next
    d.Add list1(e, 1), 1
    Debug.Print d.Item(e)
Next e
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Your code is mapping a value of 1 to each text key value, is this intentional? Try following:
Rich (BB code):
    Dim list1() As Variant
    Set d = CreateObject("scripting.dictionary")
    list1 = ThisWorkbook.Sheets("Summary").Range("A7:A22").Value
    For e = LBound(list1, 1) To UBound(list1, 1)
        d.add list1(e, 1), 1
        Debug.Print d.Item(list1(e, 1))
    Next e
 
Last edited:
Upvote 0
Unless A7:A22 contains the numbers 1, 2, 3 etc you are trying to refer to keys that don't exist. What exactly are you trying to output - the keys, or the items (which are all 1)? Assuming you want to see the keys, you could use this:

Code:
 Set d = CreateObject("scripting.dictionary")
list1 = ThisWorkbook.Sheets("Summary").Range("A7:A22").Value
For e = LBound(list1) To UBound(list1)
    d(list1(e, 1)) = 1
    Debug.Print d.keys()(e - 1)
Next e
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top